简体   繁体   English

和尚发现返回的奇异json

[英]monk find returning bizarre json

So in node I have this code, which is totally trivial...but it isn't working. 所以在节点中,我有这段代码,这简直是微不足道的……但它不起作用。

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

The above works for insertion. 以上适用于插入。 Just showing to you guys = ) ! 只是向你们展示=)!

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

Prints a ton of text that I have at the bottom. 在底部显示大量文本。

That element doesn't exist in the database, but my function for adding an element worked the other day so I am not worried about that. 该元素在数据库中不存在,但是我添加元素的功能在前一天有效,因此我不必为此担心。 The function adds it if it doesn't exist . 如果不存在,函数会添加它

All I have is the console.log call that is printing all of that. 我所拥有的只是console.log调用,它可以打印所有内容。 I was hoping to have it just print 'false' or something. 我希望它只是打印“假”之类的东西。

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

As i suggested above this is not the object from the database this is the promise that will return the object when it has finished executing 正如我上面建议的那样,这不是数据库中的对象,这是在完成执行后将返回对象的承诺

Database calls are almost allways asynchronous and especially in the case of node.js which is event driven. 数据库调用几乎总是异步的,尤其是在事件驱动的node.js中。 This means it takes time, but your console.log executes immediatly. 这意味着需要时间,但是console.log立即执行。 The result wont be there. 结果不会在那里。

If i take a look at the docs, the second param is the callback function you pass in which will retrieve the object from the query 如果我看一下文档,第二个参数是您传递的回调函数,它将从查询中检索对象

So you can either do 所以你可以做

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

or 要么

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