首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoose.js find()在find()

Mongoose.js find()在find()
EN

Stack Overflow用户
提问于 2017-10-13 00:50:24
回答 1查看 944关注 0票数 0

有没有可能在Node上找到一个与猫鼬有关的东西?

我让用户更改他们的电子邮件地址,但我必须确保电子邮件没有被任何其他用户使用之前,我保存新的电子邮件。

我想这么做:

代码语言:javascript
复制
/*************************************************************
* MY_USER - PROFILE UPDATE
*************************************************************/
app.put('/api/myuser/info', auth, function(req, res) {
  serverLog.log(req, production, {});

  // User ID
  var myUserID = req.session.passport.user._id;

  if ( myUserID && validateID(myUserID) ) {

     User.findOne({
        _id: myUserID
     }, function(err, data) {
        if (err) throw err;

        if (data == null) {
           res.sendStatus(401);
           console.log(401);
        }
        // Data
        else {

           // Update Email
           if (req.body.email) {

              // Check valid email
              if ( validateEmail(req.body.email) ) {
                 console.log('validateEmail');

                 // Check Unique Email
                 User.findOne({
                    'local.user.info.email': email
                 }, function(err, user) {

                    if(err) return err;

                    if ( user ) {
                       // Email already in use
                       res.status(400).send('ERROR: Email Already in Use');
                       return;
                    }
                    console.log('uniqueEmail ' + true);
                    // Update email
                    user.local.user.info.email = req.body.email;
                 })

              }
              // Bad Email
              else {
                 res.status(400).send('ERROR: Not a propper Email');
                 return;
              }

           }

           // SAVE USER DATA               
           if ( info_sent ) {

              user.save(function(err, data) {
                 if (err) throw err;

                 res.setHeader('Content-Type', 'application/json');
                 res.status(200).send(JSON.stringify(data.local.user.info, null, 3));
                 return;
              });
           }
           // NO INFO WAS SENT
           else {
              res.status(400).send('ERROR: No information was sent.');
              return;
           }
        }
     });
  } 
  // Bad / No User ID
  else {
     res.sendStatus(401);
  }
});

我找到了用户,然后检查电子邮件是否在使用中,你会怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2017-10-13 23:38:40

这不起作用,因为您没有将您的用户保存在回调函数中,该函数检查电子邮件是否已经存在。也要考虑用承诺来避免地狱的回调

不管怎样,你可以这样做:

代码语言:javascript
复制
// Check Unique Email
User.findOne({'local.user.info.email': email }, (err, user) => {
  if (err) throw err;
  if (user) {
    return res.status(400).send('ERROR: Email Already in Use');
  } else {  // SAVE USER DATA        
   if (info_sent) {
     user.save((err, data) => {
       if (err) throw err;
       res.setHeader('Content-Type', 'application/json');
       return res.status(200).send(JSON.stringify(data.local.user.info, null, 3));
      });
    } else {
      return res.status(400).send('ERROR: No information was sent.');
    }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46720951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档