0
var email;
findEmail = (n, un, pw, item) => {
    User.findOneAndUpdate({name: n, username: un}, {$set:{password: pw}}, (err,info) => {
      item(info.email);
    });
  };

  findEmail(name, username, newPw, mail => console.log(mail)); // email@gmail.com
 const name = req.body.name;
  const username = req.body.username;
  var email;

  findEmail = (n, un, pw, item) => {
    User.findOneAndUpdate({name: n, username: un}, {$set:{password: pw}}, (err,info) => {
      item(info.email);
    });
  };

  email = findEmail(name, username, newPw, mail => mail) ;
  console.log(email); // undefined

it's print only console i want to store findEmail result value to email how to store..?

KyleYang
  • 23
  • 1
  • 5
  • You have to pass `{new: true}` to `findOneAndUpdate` method to get in return modified document. Check this answer it will help you. https://stackoverflow.com/questions/32811510/mongoose-findoneandupdate-doesnt-return-updated-document – Waseem Ansar Dec 12 '19 at 05:54
  • `findEmail` is not returning anything. – Ashh Dec 12 '19 at 06:03
  • User.findOneAndUpdate is asynchronous function in mongoose either you have to pass call back to findEmail function or use async/await for functions. – PrasathBV Dec 12 '19 at 10:15

0 Answers0