admin管理员组文章数量:1345395
Currently using node 4.3.2 and mongo 2.6. I am attempting to get a whole collection (three documents currently in the collection). When i use this bit of code i run into an issue.
function checkUpdateTime(last_updated){
var collection = db.collection(last_updated);
collection.insert({a:1});
updateTimes = collection.find({a:1}).toArray();
}
var updateTimes = [];
checkUpdateTime('last_updated');
console.log(updateTimes);
When this code is tun updateTimes is a promise and not the array i was hoping for. The goal is the edit the array then insert it back into the collection later.The insert statement works but the retrieval of the documents simply doesn't operate the way i was expecting. I have tried quite a few versions of this code but no dice.
I guess it boils down to me wondering why a promise is being returned?
Currently using node 4.3.2 and mongo 2.6. I am attempting to get a whole collection (three documents currently in the collection). When i use this bit of code i run into an issue.
function checkUpdateTime(last_updated){
var collection = db.collection(last_updated);
collection.insert({a:1});
updateTimes = collection.find({a:1}).toArray();
}
var updateTimes = [];
checkUpdateTime('last_updated');
console.log(updateTimes);
When this code is tun updateTimes is a promise and not the array i was hoping for. The goal is the edit the array then insert it back into the collection later.The insert statement works but the retrieval of the documents simply doesn't operate the way i was expecting. I have tried quite a few versions of this code but no dice.
I guess it boils down to me wondering why a promise is being returned?
Share Improve this question asked Aug 26, 2016 at 20:16 Kammryn DancyKammryn Dancy 1351 silver badge8 bronze badges1 Answer
Reset to default 11The MongoDB driver offers two options for handling asynchronous operations:
- through callbacks that get passed by the caller
- by returning a promise to the caller
When you don't pass a callback, like in your case, it will return a promise.
So you need to make a choice here. One choice that you can't choose is "make this code run synchronously", though.
I prefer promises:
function checkUpdateTime(last_updated){
var collection = db.collection(last_updated);
return collection.insert({ a : 1 }) // also async
.then(function() {
return collection.find({ a : 1 }).toArray();
});
}
checkUpdateTime('last_updated').then(function(updateTimes) {
console.log(updateTimes);
});
You could always go a bit more fancy and use something like Promise.coroutine
, that will make your code look a bit more synchronous (even though it isn't):
const Promise = require('bluebird');
const MongoClient = require('mongodb').MongoClient;
let checkUpdateTime = Promise.coroutine(function* (db, last_updated){
let collection = db.collection(last_updated);
yield collection.insert({ a : 1 });
return yield collection.find({ a : 1 }).toArray();
});
Promise.coroutine(function *() {
let db = yield MongoClient.connect('mongodb://localhost/test');
let updateTimes = yield checkUpdateTime(db, 'foobar');
console.log(updateTimes);
})();
Or async/await
, using Babel:
const MongoClient = require('mongodb').MongoClient;
async function checkUpdateTime(db, last_updated) {
let collection = db.collection(last_updated);
await collection.insert({ a : 1 });
return await collection.find({ a : 1 }).toArray();
}
(async function() {
let db = await MongoClient.connect('mongodb://localhost/test');
let updateTimes = await checkUpdateTime(db, 'foobar');
console.log(updateTimes);
})();
本文标签: javascriptcursortoArray() returns a promise instead of arrayStack Overflow
版权声明:本文标题:javascript - cursor.toArray() returns a promise instead of array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743792822a2539890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论