I’m learning back end development and need some help in mongodb, axios
my project is a simple todo app which stores the created tasks in a mongoDB database.
link of repository is here
My problem is when I create a task, I can’t mark it as done or delete it until I refresh the page.
I checked the back end server errors and found the attached highlighted error
I also checked the front end server and found axios error as attached highlighted
By investigation, I realized that ObjectId expects to receive a hex string or integer as argument, but somehow it receives something else as indicated in the error log from axios (undefined) instead of the task id.
How can I solve that problem?
2
Answers
I found out the solution. After adding a todo task, the form is being reset by the code
All what should be done is use it inside a
.then
functionThe problem is that when you create the task, you add it to the
todos
array, so it lacks_id
, which is undefined when passed to the server, giving the error:https://github.com/HanyTaha61/MEVN-stack-project/blob/master/src/components/todo-form.vue#L118
The server returns newly created todo id, so add it when pushing to the todos list.
Try changing the above to this:
Also,
await
is not needed, because you use.then
, so you can change that throughout