0

I would like to console.log() the results of a mongodb database query without going into the mongo shell. Below is my code. The relevant parts are in bold.

router.put('/quotes/:id', (req, res, next) => {
  let personToUpdate = req.params.id;
  **console.log("old name: " + JSON.stringify(db.collection("quotes").findOne({ _id: ObjectId(personToUpdate) })) );**
  db.collection("quotes").findOneAndUpdate( 
    { _id: ObjectId(personToUpdate)},
    {$set: {
      name: req.body.name
      }
    }, function (err, object) {
      if (err) {
        console.warn(err.message);
      } else {
        **console.log("new name: " + req.body.name);**
      }
    }
  );
});

I have tried:

console.dir(), but that returns [object, object]. I have tried print() and printjson(), but those are mongo shell exclusive commands.

The end result should be that when the user changes a name through a put request, first it logs the old name that is stored in the database to the console, then at the end of the findOneAndUpdate() mongodb command to change the name, it logs the new name that the user submitted through req.body.name

4
  • Why are you logging the result of an asynchronous call? Commented Oct 16, 2017 at 13:57
  • I don't understand why that is a problem. Logging ("new name: " + req.body.name) works just fine. My difficulty is in getting the old name to log that is currently stored in the database, before the changes are made. Commented Oct 16, 2017 at 13:59
  • Please read how findOne works: mongodb.github.io/node-mongodb-native/2.2/api/… Commented Oct 16, 2017 at 14:04
  • Are you suggesting I close the database or add a callback function? Because I tried those and I am still getting undefined as a console output. Commented Oct 16, 2017 at 14:12

1 Answer 1

3

findOne is async, so you'd need to provide a callback to get the result and then put the rest of your code within that callback so that the update doesn't occur until after you've got the original document.

However, the original document is passed to the findOneAndUpdate callback by default, so you can eliminate the findOne call and get the original name from there.

router.put('/quotes/:id', (req, res, next) => {
  let personToUpdate = req.params.id;
  db.collection("quotes").findOneAndUpdate( 
    { _id: ObjectId(personToUpdate)},
    {$set: {
      name: req.body.name
      }
    }, function (err, result) {
      if (err) {
        console.warn(err.message);
      } else {
        console.log("old name: " + result.value.name);
        console.log("new name: " + req.body.name);
      }
    }
  );
});
Sign up to request clarification or add additional context in comments.

1 Comment

You are incredible! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.