How to return mongodb query result with hapi js
up vote
0
down vote
favorite
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?
node.js mongodb api hapijs
add a comment |
up vote
0
down vote
favorite
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?
node.js mongodb api hapijs
Are there any errors? Are you using the MongoDB node driver? Is the db connection successful andcollection
variable declared correctly (pointing to a valid collection)?
– Bajal
Nov 11 at 2:32
There's no error, theConsole.log
did 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;
– tab87vn
Nov 11 at 13:27
Possible duplicate of cursor.toArray() returns a promise instead of array
– Akrion
Nov 11 at 22:21
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
node.js mongodb api hapijs
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?
node.js mongodb api hapijs
node.js mongodb api hapijs
edited Nov 11 at 0:04
kmdreko
3,46011225
3,46011225
asked Nov 10 at 23:28
tab87vn
10711
10711
Are there any errors? Are you using the MongoDB node driver? Is the db connection successful andcollection
variable declared correctly (pointing to a valid collection)?
– Bajal
Nov 11 at 2:32
There's no error, theConsole.log
did 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;
– tab87vn
Nov 11 at 13:27
Possible duplicate of cursor.toArray() returns a promise instead of array
– Akrion
Nov 11 at 22:21
add a comment |
Are there any errors? Are you using the MongoDB node driver? Is the db connection successful andcollection
variable declared correctly (pointing to a valid collection)?
– Bajal
Nov 11 at 2:32
There's no error, theConsole.log
did 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;
– tab87vn
Nov 11 at 13:27
Possible duplicate of cursor.toArray() returns a promise instead of array
– Akrion
Nov 11 at 22:21
Are there any errors? Are you using the MongoDB node driver? Is the db connection successful and
collection
variable declared correctly (pointing to a valid collection)?– Bajal
Nov 11 at 2:32
Are there any errors? Are you using the MongoDB node driver? Is the db connection successful and
collection
variable declared correctly (pointing to a valid collection)?– Bajal
Nov 11 at 2:32
There's no error, the
Console.log
did show the expected result (an array of users) so I suppose the connection and query are good. Yes, I'm using MongoDB node drive const mongoClient = require('mongodb').MongoClient;
– tab87vn
Nov 11 at 13:27
There's no error, the
Console.log
did show the expected result (an array of users) so I suppose the connection and query are good. Yes, I'm using MongoDB node drive const mongoClient = require('mongodb').MongoClient;
– tab87vn
Nov 11 at 13:27
Possible duplicate of cursor.toArray() returns a promise instead of array
– Akrion
Nov 11 at 22:21
Possible duplicate of cursor.toArray() returns a promise instead of array
– Akrion
Nov 11 at 22:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You could do :
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return collection.find({}).toArray()
//return collection.findOne({}) // Or like this, to just return one result
}
});
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You could do :
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return collection.find({}).toArray()
//return collection.findOne({}) // Or like this, to just return one result
}
});
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
add a comment |
up vote
2
down vote
accepted
You could do :
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return collection.find({}).toArray()
//return collection.findOne({}) // Or like this, to just return one result
}
});
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You could do :
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return collection.find({}).toArray()
//return collection.findOne({}) // Or like this, to just return one result
}
});
You could do :
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return collection.find({}).toArray()
//return collection.findOne({}) // Or like this, to just return one result
}
});
answered Nov 11 at 22:56
Bajal
1,99411018
1,99411018
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
add a comment |
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
indeed, this works like a charm and it's simpler than I thought.
– tab87vn
Nov 12 at 11:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244432%2fhow-to-return-mongodb-query-result-with-hapi-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are there any errors? Are you using the MongoDB node driver? Is the db connection successful and
collection
variable declared correctly (pointing to a valid collection)?– Bajal
Nov 11 at 2:32
There's no error, the
Console.log
did 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;
– tab87vn
Nov 11 at 13:27
Possible duplicate of cursor.toArray() returns a promise instead of array
– Akrion
Nov 11 at 22:21