nCore nCore - 4 months ago 10
AngularJS Question

angular not getting users from mongodb

I've been trying to output user and pass on node server cmd however it keeps showing me [Object Object]. Code below should work but its not, can someone guide me on this one.

app.get('/loginDB', function (req, res){
console.log('I receive a GET req.');
db.loginDB.find(function(err, docs) {
console.log(docs+ ' get me all users');
res.json(docs);
})
});


db.loginDB.insert() is working fine but couldn't make the .find() to work, or unless I'm missing something else here. Saw a tutorial and it should be just that really.

I called the loginDB GET from postman and it returns me all the users, so not sure why this db.loginDB.find() is not working.

Output from postman when calling longinDB GET.

[
{
"_id": "578efd321c67550df897508b",
"user": "user1",
"pass": "pass1"
},
{
"_id": "578efd581c67550df897508c",
"user": "user2",
"pass": "pass2"
},
{
"_id": "578efdb29cccf62a7432f2da",
"user": "user3",
"pass": "pass3"
}
]

Answer

If you append a variable to a string, it will stringify the variable using .toString() method, so in a standar javascript object will output [object Object].

You should change this:

console.log(docs+ ' get me all users');

for this:

console.log(docs, ' get me all users');

Now you can see the javascript object, and the string correctly in console.

Comments