Reputation: 35346
I am trying to get a value from a mongodb document, but I am not getting result even if the request
is correct:
db.open(function(err, db){
var request = {
'hash' : req.params['link']
}
db.collection('urlmaps', function(error, collection){
var result = collection.find(request, {'long_url' : 1 });
console.log(JSON.stringify(result));
res.send(JSON.stringify(result));
});
});
I am expecting a var result
of type string, I am not sure of my query is enough to pull out the string from the database. Any ideas?
Upvotes: 0
Views: 2315
Reputation: 35346
I had to use collection.findOne(request, function(...))
instead of just collection.find
. That fixed the problem.
Upvotes: 1
Reputation: 85
Try this code. I think that you use find method incorrect. As I know, all nodejs mongodb drivers are async and use callbacks to return value.
db.open(function(err, db){
var request = {
'hash' : req.params['link']
}
db.collection('urlmaps', function(error, collection){
collection.find(request, {'long_url' : 1}, function(err, result){
console.log(JSON.stringify(result));
res.send(JSON.stringify(result));
})
});
});
Upvotes: 2