I've been trying to build APIs with Hapi, started with something simple as returning all users from database:
{
method: 'GET',
path: '/users',
handler: (request, h) => {
var users;
collection.find({}).toArray((err, users) => {
console.log(res)
// I want to return the list of users here
// return users // this one does not work
// return h.response(users) // does not work either
});
return "" // or here
}
}
How can I make this work?
collectionvariable declared correctly (pointing to a valid collection)?Console.logdid show the expected result (an array of users) so I suppose the connection and query are good. Yes, I'm using MongoDB node driveconst mongoClient = require('mongodb').MongoClient;