0

我正在尝试使用以下方法访问僧侣中 find 方法返回的数据:

var collection = db.get('Workflow');
collection.find({},{},function(e,workflow){
    console.log(workflow);
    var req0 = workflow[0];  //Error here
    console.log(req0);
    console.log(req0, req.body.accountType);
});

第一个 console.log(workflow) 正确返回整个记录,看起来像一个 json 数组。但是,当我尝试访问时,它给了我以下错误:

TypeError: Cannot read property '0' of undefined
at Promise.<anonymous> (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\routes\index.js:68:38)
at Promise.<anonymous> (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mpromise\lib\promise.js:178:8)
at Promise.emit (events.js:117:20)
at Promise.emit (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mpromise\lib\promise.js:84:38)
at Promise.fulfill (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mpromise\lib\promise.js:97:20)
at Promise.resolve (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mpromise\lib\promise.js:126:15)
at C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\cursor.js:163:16
at commandHandler (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\cursor.js:709:16)
at C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\db.js:1846:9
at Server.Base._callHandler (C:\Users\abchauhan\Desktop\Work\NodeLearning\HelloNode\node_modules\monk\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\base.js:445:41)
4

1 回答 1

0

因为,它不返回数组。如果您只需要第一条记录,为什么不尝试“限制”

var collection = db.get('Workflow');
collection.find({},{'limit':1},function(e,workflow){
    console.log(workflow);
    var req0 = workflow;
    console.log(req0);
});
于 2015-08-31T08:43:36.017 回答