0

I have an issue with promises.

mongoDb.getFromDb(req.body.firstName, req.body.lastName)
     .then(
           rows => {
                    console.log(rows.count()); // Promise { <pending> }
           }
      }

The log is: Promise { <pending> }.

I read about this state:

Pending - until a Promise is fulfilled it is in pending state

That's why, I use .then() method.

As I read, .then() method takes a function that will be passed the resolved value of the Promise once it is fulfilled.

So if it was fulfilled, why is the state pending?

If it's relevant, this is getFromDb function:

// Use connect method to connect to the Server
function init() {
  connectingDb = new Promise(
    function (resolve, reject) {
      MongoClient.connect(url, function (err, db) {
        if (err) {
          console.log('Unable to connect to the mongoDB server. Error:', err);
          reject(err);
        }
        else {
          console.log('Connection established to', url);

          resolve(db);
        }
      });

    }
  );
}

UPDATE #1 (torazaburo's comment):

function getFromDb(firstName, lastName) {

    return connectingDb
                       .then(myDb => {
                                      return myDb.collection('alon').find({
                                        "firstName": firstName,
                                        "lastName": lastName
                                      })
                       })
                       .catch(err=> { return err; })
}

(I'm still getting Promise { <pending> })

Update #2 (noisypixy's comment):

var mongodb = require('mongodb'); var MongoClient = mongodb.MongoClient;

Thanks.

Maria Maldini
  • 473
  • 1
  • 4
  • 7
  • 1
    Although not directly related to your problem, in `getFromDb` you do not need to say `new Promise`; you can simply `return connecdtingDb.then...`, and inside it just return the resulting value instead of calling `resolve()` with it. By the way, are you sure that `find` returns a promise? –  Sep 25 '16 at 17:49
  • You are right, it doesn't return a promise. I've updated my topic. Thanks. – Maria Maldini Sep 25 '16 at 18:08
  • 1
    I can't confirm this unless you explain what is `MongoClient`, but... are you sure `rows` is not a **cursor** or something similar? In that case it would make sense for `rows.count()` to return a promise. – noisypixy Sep 25 '16 at 18:25
  • Yes, you are right. It returns a cursor: https://docs.mongodb.com/manual/reference/method/db.collection.find/ – Maria Maldini Sep 25 '16 at 18:37
  • 2
    This question has already been asked and answered [here](http://stackoverflow.com/q/32792163/2670182) – Cool Blue Sep 25 '16 at 22:59

1 Answers1

0

Try this:

function getFromDb(firstName, lastName) {

return connectingDb
                   .then(myDb => {
                                  myDb.collection('alon').find({
                                    "firstName": firstName,
                                    "lastName": lastName
                                  })
                   })
                   .catch(err=> { return err; })
}
Sachin
  • 2,912
  • 16
  • 25