app.use(route.get('/allUsers',async(ctx)=> {
var usersList = await db.users.find({});
console.log("data",usersList);
await ctx.render('BrowseAllUsers',{userlist:usersList});
}))
this is my code and i am getting [function] as output if i write console.log(userList) but if i write console.log("data",usersList); then i m getting
data function (fn){
cb = fn;
if (results && !called) {
called = true;
fn.apply(this, results);
}
}
this output what is wrong
using
"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
how can i get list of users in userlist what is wrong with my code
I assume that db.users
is the result of co-monk
wrapping a collection.
Since co-monk
uses thunkify
, db.users.find()
will return a thunk, which async/await
cannot handle (I think by design).
Instead, you can use regular promises, which Monk supports out of the box:
// Assuming `db` is a Monk instance:
var usersList = await db.get('users').find({});
I found that you need to remove any calls to co-monk
, otherwise its effects will "linger" and the above code will fail as well.
You can probably still store a reference to the collection in db
if you like:
db.users = db.get('users');
// then, later:
let results = await db.users.find(...);