Wednesday 15 August 2012

node.js - Mongoose findOne doesn't execute -


I am trying to get data but I do not know why it does not work

  module.exports.addUser = Function (First Name, Last Name, Username, PWD, Dob, Email) {Mongoose.connect ('Mongodb: // localhost / testdb'); Var user = mongos Model ('user.profile', Profile_sema); Var newuser = new user (); Newuser.dob = dob; Newuser.name.first = firstName; Newuser.name.last = lastName; Newuser.name.user = userName; Newuser.pwd = crypto.createHash ('md5'). Update (PWD) .Digest ("HEX"); Newuser.email = Email; Newuser.save (function (error) {if (error) console.log (err); // save!}); User.findOne ({'email': 'abc@gmail.com'}, function (error, data) {console.log ('here'); console.log (data); console.log (err);}) ; Mongoose.disconnect (); };   

I use this code to add user: add code ('hii', 'heee', 'sss', '123456', '28 / 05 / 1991 ',' Adds database data to the corrected abc code (no error, I have checked the database on the console), but I can not find anything (no logs are shown) < / P>

Even if I changed the condition

  user.findOne ({}, function (mistake, data) {console.log ('here'); console.log ); Console.log (Err);});   

Can anyone help me? Thank you. In the end, I understand the problem is that nodesjesic asynchronous works; it has long been a process of taking actions. So, in my code, if I'm careless when issuing the command mongoose.disconnect , then some tasks can not be completed by doing just that command on the last callback of my work Function callback), everything worked.

  newuser.save (function (err, doc) {console.log ('nothing'); If (error) console.log (mistake); User.findOne ({'email': 'abc@gmail.com'}, function (mistake, data) {console.log ('here'); console.log (data); console.log (err); mongoose Disconnect ();}); // save!});   

Thanks for everyone's help!

No comments:

Post a Comment