简体   繁体   English

在 mongodb 和 nodejs 中承诺未决错误

[英]promise pending error in mongodb and nodejs

I have written node.js code for getting some number using mongodb database.this is my code for that我已经编写了使用 mongodb 数据库获取一些数字的 node.js 代码。这是我的代码

    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此 mongodb 查询在 mongo shell 上运行正确,但与 node.js 一起使用时出错

Promise <Pending>

I don't know what is this "promise" ?我不知道这个“承诺”是什么? Please help..请帮忙..

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. node.js 代码是异步的,因此numItems不会包含项目数 - 它包含Promise ,在解决时包含项目数。 You defenetely have to master the basics of node.js and asynchronous programming.你必须掌握 node.js 和异步编程的基础知识。 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对于原生Promise ,请查看文档https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Promise

Also look at bluebird promises https://github.com/petkaantonov/bluebird另请查看bluebird承诺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.或者更好的是,从您的函数返回承诺,并让他们使用 Promises 模式而不是回调模式来实现它。

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.我开车试图解决一个类似的问题,无论我做什么, document.save()选项都会给出Promise{pending} Here's what I did:这是我所做的:

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

Finally, log in to MongoDB web, and change accessible IP addresses to 0.0.0.0 (all addresses).最后,登录 MongoDB web,将可访问的 IP 地址更改为0.0.0.0 (所有地址)。 Not doing this can cause issues sometimes even when your IP is whitelisted.即使您的 IP 被列入白名单,不这样做有时也会导致问题。

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 ) (添加await并将此功能转换为async function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM