最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - promise pending error in mongodb and nodejs - Stack Overflow

programmeradmin5浏览0评论

I have written node.js code for getting some number using mongodb database.this is my code for that

    MongoClient.connect('mongodb://localhost:27017/mongomart', function(err, db) {

    assert.equal(null, err);

    var numItems=db.collection('item').find({"category":category}).count();

    callback(numItems);
});

This mongodb query runs correct on mongo shell but it is giving error when using with node.js

Promise <Pending>

I don't know what is this "promise" ? Please help..

I have written node.js code for getting some number using mongodb database.this is my code for that

    MongoClient.connect('mongodb://localhost:27017/mongomart', function(err, db) {

    assert.equal(null, err);

    var numItems=db.collection('item').find({"category":category}).count();

    callback(numItems);
});

This mongodb query runs correct on mongo shell but it is giving error when using with node.js

Promise <Pending>

I don't know what is this "promise" ? Please help..

Share Improve this question asked Feb 24, 2016 at 14:13 Udit KumawatUdit Kumawat 6841 gold badge8 silver badges23 bronze badges 1
  • How is that an error? Looks like a return value on the REPL to me. – Bergi Commented Feb 24, 2016 at 14:19
Add a comment  | 

5 Answers 5

Reset to default 9

node.js code is asynchronous so that numItems won't contain count of items - it rather contains Promise that contains count of items when resolved. You definitely have to master the basics of node.js and asynchronous programming. Try to modify your code like this

MongoClient.connect('mongodb://localhost:27017/mongomart', function(err, db) {
  assert.equal(null, err);
  db.collection('item').find({"category":category}).count()
    .then(function(numItems) {
      console.log(numItems); // Use this to debug
      callback(numItems);
    })
});

For native Promise check out documentation https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Promise

Also look at bluebird promises https://github.com/petkaantonov/bluebird

A promise is a substitute temporary value that is given while you wait on the real value. To get the real value do

numItems.then(function (value) { callback(value) });

Or better yet, return the promise from your function, and let they implement it using the Promises pattern, instead of the callback pattern.

Had the same problem. Don't know if it's still relevant to you, but this is what solved it for me:

var category = 'categoryToSearch';
var cursor = db.collection('item').find({'category':category});

cursor.count(function (err, num) {
    if(err) {
        return console.log(err);
    }
    return num;
});

I drove myself bananas trying to solve a similar problem, where the document.save() option just gave Promise{pending} no matter what I did. Here's what I did:

  • Change (req,res) to async(req,res).
  • Change var post = doc.save() to var post = await doc.save().

Finally, log in to MongoDB web, and change accessible IP addresses to 0.0.0.0 (all addresses). Not doing this can cause issues sometimes even when your IP is whitelisted.

try this:

MongoClient.connect('mongodb://localhost:27017/mongomart', async (err, db) => {

    assert.equal(null, err);

    var numItems= await db.collection('item').find({"category":category}).count();

    callback(numItems);
});

(adding the await and turn this function to async function)

发布评论

评论列表(0)

  1. 暂无评论