node.js - mongoose findOne data does not exit not working -


user_id 2 not exist in mongodb console log not print 'does not exist'

var query = postdata.findone({ 'user_id': '2'});  query.exec(function (err, doc) {   if(doc) {     console.log('ok');   } else {     console.log('does not exist');   } }); 

without mongoose works:

connection.db.collection("postdata", function(err, collection){    collection.find({ 'user_id': '2'}).toarray(function(err, data){       console.log(data); // print collection data    }) }); 

prints []

no matter object exists or not , mongodb going return array anyway. if object exists, array filled otherwise it's empty array. if want check if user exists or not must check doc.length , if it's 0 means user doesn't exist.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -