gpt4 book ai didi

node.js - nodejs koajs 异步等待未从 db.collection.find 获取值

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:08 25 4
gpt4 key购买 nike

    app.use(route.get('/allUsers',async(ctx)=> {
var usersList = await db.users.find({});

console.log("data",usersList);

await ctx.render('BrowseAllUsers',{userlist:usersList});
}))

这是我的代码,如果我写 console.log(userList) 但如果我写 console.log("data",userList); 我会得到 [function] 作为输出。然后我就得到了

data function (fn){
cb = fn;

if (results && !called) {
called = true;
fn.apply(this, results);
}
}

这个输出有什么问题

使用

"dependencies": {
"babel-core": "^6.13.2",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-stage-0": "^6.5.0",
"co": "^4.6.0",
"co-monk": "^1.0.0",
"koa": "^2.0.0-alpha.5",
"koa-bodyparser": "^3.2.0",
"koa-convert": "^1.2.0",
"koa-generic-session": "^1.11.3",
"koa-passport": "^2.2.2",
"koa-route": "^3.1.0",
"koa-static": "^3.0.0",
"koa-validate": "^1.0.6",
"koa-views": "^5.0.2",
"monk": "^3.1.1",
"passport-google-auth": "^1.0.1",
"passport-local": "^1.0.0",
"swig": "^1.4.2"

}

AND Node --version v4.5.0

我如何获取用户列表中的用户列表我的代码有什么问题

最佳答案

我假设db.usersco-monk包装集合的结果。

因为co-monk使用thunkify , db.users.find() 将返回 thunkasync/await 无法处理(我认为是设计使然)。

相反,您可以使用 Monk 开箱即用的常规 Promise:

// Assuming `db` is a Monk instance:
var usersList = await db.get('users').find({});

我发现您需要删除对 co-monk 的任何调用,否则其效果将“徘徊”,并且上面的代码也会失败。

如果您愿意,您可能仍然可以在 db 中存储对该集合的引用:

db.users = db.get('users');

// then, later:
let results = await db.users.find(...);

关于node.js - nodejs koajs 异步等待未从 db.collection.find 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com