Node.js TypeError:无法读取属性';发送';nodejs中未定义的
我正在使用Angular6、mongodb和nodejs开发一个注册表单。在那里,我编写了一个post方法,用于在数据库中不存在用户时在mongodb中保存用户。将用户添加到数据库时,应向用户发送一封电子邮件,用户应重定向到另一个视图。该视图也在早期的html中,仅当结果为成功时才会显示。如果电子邮件名称已在数据库中,则应显示错误消息。我在密码中使用了默认错误消息。strategy-options.ts用于现有用户的错误消息。但当我尝试添加新用户时,它不会导航到下一个视图,并且终端显示以下错误消息。 TypeError:无法读取未定义的属性“send” “…节点\u模块\mongodb\lib\utils.js:132” 这是我的保存方法Node.js TypeError:无法读取属性';发送';nodejs中未定义的,node.js,angular,mongodb,Node.js,Angular,Mongodb,我正在使用Angular6、mongodb和nodejs开发一个注册表单。在那里,我编写了一个post方法,用于在数据库中不存在用户时在mongodb中保存用户。将用户添加到数据库时,应向用户发送一封电子邮件,用户应重定向到另一个视图。该视图也在早期的html中,仅当结果为成功时才会显示。如果电子邮件名称已在数据库中,则应显示错误消息。我在密码中使用了默认错误消息。strategy-options.ts用于现有用户的错误消息。但当我尝试添加新用户时,它不会导航到下一个视图,并且终端显示以下错误消
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: 't36@gmail.com',
pass: '12345'
}
});
var mailOptions = {
from: 'reg@demo.com',
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参数,在您的情况下next
表现为res
参数。
其次,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: 't36@gmail.com',
pass: '12345'
}
});
var mailOptions = {
from: 'reg@demo.com',
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}");
}
});
});
为了解决相同的错误消息TypeError:Cannot read property'send'of undefined在我的rest api应用程序中,我发现我错过了有效的语法
res.status(200).send(data)
或res.send(data)
。虽然我在控制台中找到了数据。
使用res.status()
时,必须使用.send()
,然后再使用res
我认为这对犯同样错误的开发人员也有帮助。快乐的开发者
module.exports.getUsersController = async (req, res) => {
try {
// Password is not allowed to pass to client section
const users = await User.find({}, "-password");
const resData = {
users,
success: {
title: 'All Users',
message: 'All the users info are loaded successfully.'
}
}
console.log(resData)
// This is not correct
// return res.status(200).res.send(resData);
// It should be
return res.status(200).send(resData);
} catch (err) {
console.log(err)
return res.status(500).send(err);
}
};