0

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.

Arjun Dass
  • 11
  • 3
  • 1
    Are there any error shown in the console? What does the `console.log` of `NewTask2.then( ... )` output (`undefined`, `null`, ... ) or isn't it called at all? – t.niese Oct 02 '16 at 17:59
  • @t.niese Its not getting called at all. The console is showing the new task that has been added and all the previous tasks. – Arjun Dass Oct 02 '16 at 18:04
  • 1
    Is anything reported if you write `todo.createTask("Test", "Test").catch(err => { console.log(err); } )` instead of `todo.createTask("Test", "Test")`? – t.niese Oct 02 '16 at 18:10
  • What does `todo()` do? – Bergi Oct 02 '16 at 18:18
  • What do you mean by "not able to reach the second function" - the `NewTask2.then` callback function? – Bergi Oct 02 '16 at 18:18
  • 1
    @t.niese I just add the catch statement you suggested and its showing this error [MongoError: E11000 duplicate key error collection: lab3.todoItems index: _id_ dup key: { : "07479420-88d2-11e6-bb7d-f3cf91da5090" }] name: 'MongoError', message: 'E11000 duplicate key error collection: lab3.todoItems index: _id_ dup key: { : "07479420-88d2-11e6-bb7d-f3cf91da5090" }', driver: true, index: 0, code: 11000, errmsg: 'E11000 duplicate key error collection: lab3.todoItems index: _id_ dup key: { : "07479420-88d2-11e6-bb7d-f3cf91da5090" }' } – Arjun Dass Oct 02 '16 at 18:58
  • @Bergi todo is a variable for requiring the todo.js file which includes both the createTask and getAllTasks function. – Arjun Dass Oct 02 '16 at 19:01
  • 1
    I meant the `todo()` call *inside* the `createTask`/`getAllTasks` functions which might have been cause for a problem, but it doesn't matter any more - by installing an error handler you already found the problem – Bergi Oct 02 '16 at 19:04

0 Answers0