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
Post a Comment