Samuel Tucker

Samuel Tucker

1550033287

MongoDB Node Driver UpdateOne Promise Parameters

I am confused about the parameters that are passed through to the promise function of write operations.

For example, the docs for updateOne say that the callback is an instance of updateWriteOpCallbackwhich has parameters error and result.

db.collection.('my-collection')
    .updateOne({_id: someObjectID}, {$set: someChanges}, function(err, result){
        console.log(err); //null
        console.log(result); //CommandResult
    }

In the above example, if the operation is successful, err is correctly set to null and result contains the result.

However, if I choose to us a promise instead of a callback:

db.collection.('my-collection')
    .updateOne({_id: someObjectID}, {$set: someChanges})
    .then((err, result) => {
        console.log(err); //CommandResult
        console.log(result); //undefined
    }

The first parameter err is actually giving me what should be in result, and result is undefined. Is there an explanation for why this happens?

#node-js #mongodb

What is GEEK

Buddha Community

This is because that isn’t how promises work. When a promise gets rejected, you are supposed to catch it with the promise.catch() method, not promise.then().

So:

  • promise.then() is used to get the result of the resolve() operation.
  • promise.catch() is used to get the result of the reject() operation.

Thus your code should be:

db.collection.('my-collection')
    .updateOne({_id: someObjectID}, {$set: someChanges})
    .then((result) => {
        console.log(result);
    }).catch((err) => {
        console.log(err);
    });

Query of MongoDB | MongoDB Command | MongoDB | Asp.Net Core Mvc

https://youtu.be/FwUobnB5pv8

#mongodb tutorial #mongodb tutorial for beginners #mongodb database #mongodb with c# #mongodb with asp.net core #mongodb

Install MongoDB Database | MongoDB | Asp.Net Core Mvc

#MongoDB
#Aspdotnetexplorer

https://youtu.be/cnwNWzcw3NM

#mongodb #mongodb database #mongodb with c# #mongodb with asp.net core #mongodb tutorial for beginners #mongodb tutorial

Samuel Tucker

Samuel Tucker

1550033287

MongoDB Node Driver UpdateOne Promise Parameters

I am confused about the parameters that are passed through to the promise function of write operations.

For example, the docs for updateOne say that the callback is an instance of updateWriteOpCallbackwhich has parameters error and result.

db.collection.('my-collection')
    .updateOne({_id: someObjectID}, {$set: someChanges}, function(err, result){
        console.log(err); //null
        console.log(result); //CommandResult
    }

In the above example, if the operation is successful, err is correctly set to null and result contains the result.

However, if I choose to us a promise instead of a callback:

db.collection.('my-collection')
    .updateOne({_id: someObjectID}, {$set: someChanges})
    .then((err, result) => {
        console.log(err); //CommandResult
        console.log(result); //undefined
    }

The first parameter err is actually giving me what should be in result, and result is undefined. Is there an explanation for why this happens?

#node-js #mongodb

Insert & Save method in MongoDB | Inserting Document | CRUD Operation in MongoDB

#MongoDB
#AspDotNetExplorer
https://youtu.be/CohnNdE_rjM

#mongodb #mongodb tutorial for beginners #mongodb tutorial #mongodb database #learn mongodb