【发布时间】:2019-09-12 22:31:27
【问题描述】:
我正在使用 node 和 express 为我的应用程序创建服务器。这就是我的代码的样子:
async function _prepareDetails(activityId, dealId) {
var offerInfo;
var details = [];
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
const offers_collection = db.collection('collection_name');
await offers_collection.aggregate([
{ "$match": { 'id': Id} },
]).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
details = docs;
});
})
return details;
}
app.post('/getDetails',(request,response)=>{
var Id = request.body.Id;
var activityId = request.body.activityId;
_prepareDetails(activityId,Id).then(xx => console.log(xx));
response.send('xx');
})
在调用 getDetails API 时,我得到了
await is only valid in async function error (At line await offers_collection.aggregate)
我在声明async function 时也出现了红色下划线。我使用的节点版本是 11.x。我也在使用firebase API。我在这里做错了什么?
【问题讨论】:
-
function(err) {应该是async? -
我相信
toArray产生普通数组而不是数组的Promise,所以等待它对我来说真的没有意义。
标签: javascript node.js mongodb express async-await