我正在使用Angular6,mongodb和nodejs开发一个注册表单。如果数据库中不存在用户,我已经编写了一个post方法来保存mongodb中的用户。将用户添加到数据库后,应向用户发送电子邮件,用户应重定向到另一个视图。该视图也在早期的html中,它仅在结果成功时显示。如果电子邮件名称已经在数据库中,则应显示错误消息。我在密码中使用了默认错误消息。 strategy-options.ts用于现有用户的错误消息。但是当我尝试添加新用户时,它不会导航到下一个视图,并且终端显示以下错误消息。 TypeError:无法读取未定义的属性'send'“...... node_modules \ mongodb \ lib \ utils.js:132”
这是我的保存方法。
router.post('/signup', function(req, next) {
console.log("Came into register function.");
var newUser = new userInfo({
firstName : req.body.firstName,
lastName : req.body.lastName,
rank : req.body.lastName,
mobile : req.body.lastName,
email : req.body.email,
userName : req.body.userName,
password : req.body.password,
status : req.body.status
});
newUser.save(function (err, user,res) {
console.log("Came to the save method");
if (err){
console.log(user.email);
res.send(err);
return res;
}
else{
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: '12345'
}
});
var mailOptions = {
from: '[email protected]',
to: newUser.email,
subject: 'Send mails',
text: 'That was easy!'
};
console.log("This is the user email"+" "+newUser.email);
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log("Error while sending email"+" "+error);
} else {
console.log('Email sent: ' + info.response);
}
});
console.log("success");
return res.send("{success}");
}
});
});
这是我在register.component.ts文件中的register方法。
register(): void {
this.errors = this.messages = [];
this.submitted = true;
this.service.register(this.strategy, this.user).subscribe((result: NbAuthResult) => {
this.submitted = false;
if (result.isSuccess()) {
this.messages = result.getMessages();
this.isShowConfirm = true;
this.isShowForm = false;
}
else {
this.errors = result.getErrors();
}
const redirect = result.getRedirect();
if (redirect) {
setTimeout(() => {
return this.router.navigateByUrl(redirect);
}, this.redirectDelay);
}
this.cd.detectChanges();
});
}
我在互联网上尝试了很多方法来解决这个问题。但仍然没有。
首先,节点js路由器由3个参数req, res, next
组成,你错过了res param,在你的情况下,next
表现为res
params。其次Model.save只返回错误并保存数据,其中没有res参数。所以最终的代码看起来像这样
router.post('/signup', function(req, res, next) {
console.log("Came into register function.");
var newUser = new userInfo({
firstName : req.body.firstName,
lastName : req.body.lastName,
rank : req.body.lastName,
mobile : req.body.lastName,
email : req.body.email,
userName : req.body.userName,
password : req.body.password,
status : req.body.status
});
newUser.save(function (err, user) {
console.log("Came to the save method");
if (err){
console.log(user.email);
res.send(err);
return res;
}
else{
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: '12345'
}
});
var mailOptions = {
from: '[email protected]',
to: newUser.email,
subject: 'Send mails',
text: 'That was easy!'
};
console.log("This is the user email"+" "+newUser.email);
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log("Error while sending email"+" "+error);
} else {
console.log('Email sent: ' + info.response);
}
});
console.log("success");
return res.send("{success}");
}
});
});