0

i just searched for my problems around in the stackoverflow's discussions but nothings similar with my issued. So, in this case, i just want to update my collection and then use the 'Promise' module instead use the callbacks / anonymous functions as normally i did. but the error comes up when I execute the js's application in cmd.

Here my simple code:

var Promise = require('promise'); // use the 'promise' module
    var MongoClient = require('mongodb').MongoClient;
    var url = 'mongodb://localhost/EmployeeDB';

    MongoClient.connect(url)
     .then(function(err, db) {
         db.collection('Employee').updateOne({
             "EmployeeName": "Jeffrey"
         }, {
             $set: {
                 "EmployeeName": "Jeffrey Scurgs"
             }
         });
     }); 

and the error results when i executed the code in cmd:

(node:8600) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'collection' of undefined at C:\Users\DELL\guru99\5_Promise\app_promise.js:7:9 at at process._tickCallback (internal/process/next_tick.js:189:7) (node:8600) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:8600) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

so, is there any wrong code in my code above?

thanks for helping... sorry for my bad english

Joe Wilson
  • 25
  • 1
  • 11

2 Answers2

1

Your approach is correct but you a missed one thing.

When you don't provide callback parameters mongo ORM return a promise. Here is the corrected code:

MongoClient.connect(url)
    .then(function(db) { // <- db is first argument
        db.collection('Employee').updateOne({
            "EmployeeName": "Jeffrey"
        }, {
            $set: {
                "EmployeeName": "Jeffrey Scurgs"
            }
        });
    })
    .catch(function (err) {})
uday singh
  • 53
  • 5
  • so.. what if I nested the Promise like **MongoClient.connect(url) .then(function(db) { db.collection('Employee').insertOne({ Employeeid: 4, EmployeeName: "NewEmployee" }) .then(function(db1) { db1.collection('Employee').insertOne({ Employeeid: 5, EmployeeName: "NewEmployee1" }); }); });** should I added the catch twice? – Joe Wilson Mar 09 '19 at 12:16
  • Hi..!! Is your query resolved about multiple return promises ? – uday singh Mar 12 '19 at 07:55
  • Exactly man... helping by Bergi in comment below... thanks btw – Joe Wilson Mar 12 '19 at 08:36
0

The db variable can only be undefined if there was an error connecting to the database.

You need to check and fix any error shown. You can also share the error

var Promise = require('promise'); // use the 'promise' module
    var MongoClient = require('mongodb').MongoClient;
    var url = 'mongodb://localhost/EmployeeDB';

    MongoClient.connect(url)
     .then(function(err, db) {
            if (err) {
              return throw err; // Check the error
            }
         db.collection('Employee').updateOne({
             "EmployeeName": "Jeffrey"
         }, {
             $set: {
                 "EmployeeName": "Jeffrey Scurgs"
             }
         });
     }); 
Mzndako
  • 72
  • 1
  • 4