I have a method for inserting a task in the mongoDB database and a function to display all the tasks.
createTask(title, description) {
if (!title) {
return Promise.reject("you must provide a name for your Task");
}
if (!description) {
return Promise.reject("You must provide description for your task");
}
return todo().then((todoItemsCollection) => {
let newTask = {
_id: uiud.v1(),
title: title,
description: description,
completed: false,
completedAt: null
};
return todoItemsCollection
.insertOne(newTask)
.then((newInsertInformation) => {
return newInsertInformation.insertedId;
})
.then((newId) => {
console.log("New task added");
return this.getTask(newId);
});
});
},
getAllTasks() {
return todo().then((todoItemsCollection) => {
console.log("\nHere are All your Tasks");
return todoItemsCollection.find().toArray((err, docs) => {
docs.forEach((docs) => {
console.log("\n");
console.log(docs);
});
});
});
}
I can successfully insert the first task in the database and even able to fetch all the tasks from the database. But I am unable to insert the second task "Test" with the following code in app.js.
let NewTask = todo.createTask("Ponder7 Dinosaurs", "Has Anyone Really Been Far Even as Decided to Use Even Go Want to do Look More Like?");
let taskAdded = NewTask.then((task) => {
console.log(task);
});
let allTasks = taskAdded.then(() => {
return todo.getAllTasks();
});
let NewTask2 = todo.createTask("Test", "Test");
let NewTask2 = todo.createTask("Test", "Test").catch((err) => {
console.log(err);
});
When I try to catch the error, It says
{ [MongoError: E11000 duplicate key error collection: lab3.todoItems index: _id_ dup key: { : "11477162-4844-4f5d-aae8-1afb8a560250" }]
name: 'MongoError',
message: 'E11000 duplicate key error collection: lab3.todoItems index: _id_ dup key: { : "11477162-4844-4f5d-aae8-1afb8a560250" }',
driver: true,
index: 0,
code: 11000,
errmsg: 'E11000 duplicate key error collection: lab3.todoItems index: _id_ dup key: { : "11477162-4844-4f5d-aae8-1afb8a560250" }' }
When I am doing the same thing with mongoDB ObjectID , the second function is getting called but it gives the above error while using node-uuid module.