【问题标题】:Object #<Promise> has no method 'limit'对象 #<Promise> 没有方法“限制”
【发布时间】:2015-11-14 19:22:20
【问题描述】:

当我运行代码时

  var collection = db.get('categories');
 console.log(collection.find().limit(1).sort( { _id : -1 } ));

在 nodejs 上使用 mongodb 我收到错误 Object # has no method 'limit' 。我是 node 的初学者,真的卡在 node 的这一部分

这里是获取最后一个插入文档的完整代码。

router.post('/addcategory', function(req, res) {

// Set our internal DB variable
var db = req.db;

// Get our form values. These rely on the "name" attributes
var name = req.body.name;
var description = req.body.description;




// Set our collection
var collection = db.get('categories');

// Submit to the DB
collection.insert({

    "name" : name,
    "description" : description,


}, function (err, doc) {
    if (err) {
        // If it failed, return error
        res.send("There was a problem adding the information to the database.");
    }
    else {
        // And forward to success page
        /******************/
        console.log(collection.find().limit(1).sort( { _id : -1 } ));


        /*************/
    }
});
});

【问题讨论】:

  • 你用的是什么版本的mongodb驱动?把var collection = db.get('categories');改成var collection = db.collection('categories');有帮助吗?
  • mongodb 使用什么模块?
  • @JoGoFo 当我这样做时,我收到此错误Object #&lt;Manager&gt; has no method 'collection'
  • @mscdex 我正在使用 mongo 和和尚,但我不确定它是否正确 var mongo = require('mongodb'); var monk = require('monk'); var db = monk('localhost:27017/glass')

标签: node.js mongodb mongodb-query


【解决方案1】:

这里缺少的关键信息是您使用的是 Monk,而不是本机 MongoDB Node.JS 驱动程序。 find() 的命令是您将如何使用本机驱动程序(上面@BlakesSeven 建议的更改以实现异步),但 Monk 的工作方式略有不同。

试试这个:

collection.find({}, { limit : 1, sort : { _id : -1 } },  function (err,res) { 
    console.log(res);
});

【讨论】:

    【解决方案2】:

    该方法仍然是异步的,因此您仍然需要将其作为带有.then() 的承诺或回调来调用。没有任何方法是同步的并内联返回结果。

    此外,从驱动程序返回的结果是“光标”,而不是您期望的对象。您要么迭代返回的光标,要么只使用 .toArray() 或类似的转换:

    collection.find().limit(1).sort({ "_id": -1 }).toArray().then(function(docs) {
        console.log(docs[0]);
    });
    

    或者:

    collection.find().limit(1).sort({ "_id": -1 }).toArray(function(err,docs) {
        console.log(docs[0]);
    });
    

    但实际上整个前提是不正确的。您似乎基本上想返回您刚刚插入的内容。在您的代码中进行更正的事件,返回的文档不一定是您刚刚插入的文档,而是插入到集合中的最后一个文档,这可能来自另一个操作或从另一个来源调用此路由。

    如果您想要插入的内容,请调用.insertOne() 方法并检查结果:

    collection.insertOne({ "name": name, "description": description },function(err,result) {
      if (err) {
        res.send("There was an error");
      } else {
        console.log(result.ops)
      }
    });
    

    .insert() 方法被认为已弃用,但基本上返回相同的内容。考虑是它们返回一个insertWriteOpResult 对象,其中ops 属性包含插入的文档及其_id 值,

    【讨论】:

    • 我在输入您的代码时收到此错误TypeError: Object #&lt;Promise&gt; has no method 'limit'。我不知道我错在哪里。
    • @Mohammed 是的,这是您的问题,这就是答案。您尝试在代码中返回的对象不是“结果”而是“承诺对象”,因此是错误。这向您展示了如何通过更改代码从该对象获取结果。它还解决了您似乎希望返回的对象是您刚刚插入的对象。您的代码不太可能如此,还有另一种方法来确保它被列出。
    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 2017-05-03
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2014-05-17
    相关资源
    最近更新 更多