ID is undefined when trying to match IDs (React, express/mongodb)

Viewed 37

So it appears the problem lies in that when I call my api and console log the id in the getTodo it gets undefined.

code where the the id is undefined with console.log(data._id) console.log(data) i can see everything

0 : createdAt : "2022-09-14T20:56:54.457Z" description : "okay dokay" title : "Fix This" updatedAt : "2022-09-14T20:56:54.457Z" __v : 0 _id : "632240161ebbc57954498150" [[Prototype]] : Object
 const getTodo = async() => {
    const response = await fetch(url)
    const data = await response.json()
    setTodo(data)
    console.log(data._id)
  }

The code above it

const url = "api"
  
  const [todo, setTodo] = useState([])

the id is going here/ change here. It's supposed to go to a single todo based on its id.

const updateTodo = async (todo) => {
  const responce = await fetch(url + todo._id + "/", {
    method: "put",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(todo)
  })
  getTodo()
}
console.log(todo._id)
const deleteTodo = async (todo) => {
  const response = await fetch(url + todo._id + "/", {
    method: "delete",
  })
  getTodo()
  navigate("/")
}

and finally the gets from the back end.. the originator. Mongodb creating the id.

router.get('/', async(req, res) => {
   res.json(await Todo.find({}).catch((err) => res.status(400).json(err)))
})

router.get('/:id', async(req, res) =>{ 
    ((await Todo.findById(req.params.id)
        .then( todo => res.json(todo))
        .catch(err => res.status(400).json(error))
    ))})

I don't know what's going on, can anyone explain it?

0 Answers
Related