和尚找回离奇的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 

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

数据库调用几乎都是asynchronous的,特别是在事件驱动的node.js情况下。 这意味着需要时间,但是你的console.log立即执行。 结果不会在那里。

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

所以你可以做

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

要么

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