I want to get a user from MongoDB. so I should use the async function to get the right result. but it return me [object , promise]. how can I use this data?
async function getUsers() {
var allUsers = null;
await MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db('mydb');
dbo.collection("users").find({})
.toArray(function(err, results) {
if (err)
throw error;
db.close();
allUsers = results;
});
});
}
app.get('/user/', function(req, res) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db('mydb');
var id = req.params.id;
var allUsers = getUsers();
res.render('pages/user', {users: allUsers });
db.close();
});
});
awaitor.thenand mark your function asasync.