簡體   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