繁体   English   中英

和尚发现返回的奇异json

[英]monk find returning bizarre json

所以在节点中,我有这段代码,这简直是微不足道的……但它不起作用。

var collection = db.get('fisforfriends');
var db = monk('localholst:27017/fisforfriends');
...
var userName = req.body.username;

以上适用于插入。 只是向你们展示=)!

console.log(collection.find({}, {username: userName}));

在底部显示大量文本。

该元素在数据库中不存在,但是我添加元素的功能在前一天有效,因此我不必为此担心。 如果不存在,函数会添加它

我所拥有的只是console.log调用,它可以打印所有内容。 我希望它只是打印“假”之类的东西。

9 Dec 22:54:27 - [nodemon] starting `node app.js`
Express server listening on port 3000
GET / 200 319ms - 427b
GET /stylesheets/style.css 304 4ms
{ col:
   { manager:
      { driver: [Object],
        collections: [Object],
        options: [Object],
        _events: {} },
     driver:
      { emitter: [Object],
        state: 0,
        _dbconn: [Object],
        db: null,
        username: '',
        password: undefined,
        admin: [Object],
        _collections: [Object],
        bson_serializer: [Object],
        ObjectID: [Object] },
     name: 'fisforfriends',
     col:
      { emitter: [Object],
        state: 0,
        options: undefined,
        skinDb: [Object],
        ObjectID: [Object],
        collectionName: 'fisforfriends',
        collection: null,
        internalHint: null,
        hint: [Getter/Setter] },
     options: {} },
  type: 'find',
  completed: false,
  opts: { username: 'fa', fields: {}, safe: true },
  _events:
   { error: { [Function: g] listener: [Function] },
     success: { [Function: g] listener: [Function] } },
  fulfill: [Function],
  query: {} }
9 Dec 22:54:45 - [nodemon] restarting due to changes...
9 Dec 22:54:45 - [nodemon] C:\Users\hassan\Documents\Hassans_Bravery\fisforfrien
ds\routes\index.js

正如我上面建议的那样,这不是数据库中的对象,这是在完成执行后将返回对象的承诺

数据库调用几乎总是异步的,尤其是在事件驱动的node.js中。 这意味着需要时间,但是console.log立即执行。 结果不会在那里。

如果我看一下文档,第二个参数是您传递的回调函数,它将从查询中检索对象

所以你可以做

users.find({}).on('success', function (doc) { /* doc is the result available here */ });

要么

users.find({}, function (err, docs){ /* doc is the result available here */ });

暂无
暂无

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

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