我无法使用我的代码在数据库中更新我的文档,但我能够在数据库中明确地完成它

时间:2016-06-03 08:56:29

标签: node.js mongodb express mongoose

我已经写了一个代码来在客户注册时发送邮件。一旦他们注册,他们将收到邮件,发送邮件的代码如下。

这是我的控制器

module.exports = (function(){
return {
    newRegister: function(request,response){
        var registers = new register(request.body);
        registers.save(function(error,result){
            if(error){
                response.send(error)
                console.log(error)
            }
            else{
                response.json(result)
                var smtpTransport = nodemailer.createTransport("SMTP",{
                service: "Gmail",
                auth: {
                    user: "some@gmail.com",
                    pass: "*******"
                }
                });

                // setup e-mail data with unicode symbols
                var mailOptions = {
                    from: "some@gmail.com", // sender address
                    to: result.email, // list of receivers
                    subject: "Hello", // Subject line
                    //text: "Hello world ✔", // plaintext body
                    html: "<!DOCTYPE html><html lang='en'><head><script type='text/javascript'></script></head><body><p style=font-family:'Times New Roman'>Please click the below link for verification</p><p style=font-family:'Times New Roman'><a href='http://127.0.0.1:3060/email/:id'>Click Here</a></p></body></html>"
                }

                // send mail with defined transport object
                smtpTransport.sendMail(mailOptions, function(error, response){
                    console.log("Message inside auth: " );

                    if(error){
                        console.log(error);
                    }else{
                        console.log("Message sent: " + response.message);
                    }

                    // if you don't want to use this transport object anymore, uncomment following line
                    //smtpTransport.close(); // shut down the connection pool, no more messages
                    });
            }
        })
    }
 }
})();

这是我在单击链接时编写的更新方法。

changeStatus: function(request, response){
      register.findOneAndUpdate({"email": request.params.id}, {$set: {"status": true}}, {returnNewDocument: true}, function(error,result){
          if(error){
              response.send(error)
              console.log(error)
          }
          else{
              response.json(result)
              console.log(result)
          }
      })

    }

这是我的路线

app.get('/email/:id', function(request,response){
registercontroller.changeStatus(request,response)
response.send("User activated")
})

点击链接时我输出

User activated
null

我无法找到出错的地方。

1 个答案:

答案 0 :(得分:0)

您的功能应如下所示: -

changeStatus: function(request, response)
 {
  register.findOneAndUpdate({"email": request.params.id},
                            {$set: {"status": true}}, 
                             {new: true},//Made changes here. 
  function(error,result)
  {
      if(error)
       {
          response.send(error)
          console.log(error)
      }
      else
      {
          response.json(result)
          console.log(result)
      }
  })
}

在查询中使用new : true来获取修改后的记录。有关如何使用的详细信息,请参阅doc

希望这会有所帮助。