【问题标题】:Mongoose.js find() inside a find()在 find() 中的 Mongoose.js find()
【发布时间】:2018-03-25 01:27:38
【问题描述】:

是否可以使用 Mongoose 在 Node 上的查找中进行查找?

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

我想这样做:

/*************************************************************
* 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);
  }
});

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

【问题讨论】:

标签: javascript node.js mongodb mongoose


【解决方案1】:

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

无论如何,你可以这样做:

// 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.');
    }
 }

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 1970-01-01
    • 2013-12-17
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多