Answer the question
In order to leave comments, you need to log in
How to send data from mongodb?
I am using mongodb driver.
There is a base 'qq' and a collection 'bb' .
router.get('/1',(ctx) => {
mongoClient.connect(url, function(err, client){
client.db("qq").collection("bb").find({}).toArray((err, users)=>{
ctx.body = users
console.log(users)
client.close()
})
})
})
ctx.body = users
Answer the question
In order to leave comments, you need to log in
as far as I understand koa?
in order for koa to wait for your asynchronous action in the route, you need to return a promise:
router.get('/1', ctx => new Promise(resolve => {
mongoClient.connect(url, (err, client) => {
client.db("qq").collection("bb").find({}).toArray((err, users) => {
ctx.body = users;
console.log(users);
client.close();
resolve();
});
});
}));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question