错误:TypeError: Cannot call method 'replace'的定义
err: TypeError: Cannot call method 'replace' of undefined
我试图通过POST请求发送表单:
客户端angular.module('app').controller('contactCtrl', function($scope, $http) {
$scope.envoyer = function(nom, organisation, courriel, telephone, message){
$http.post('/contact', {nom:nom, organisation:organisation, courriel:courriel, telephone:telephone, message:message}).then(function(error, response){
console.log('sent!');
if(response.data.success){
console.log('sent!');
}
});
}
});
服务器端app.post('/contact', function(req, res, next){
var subject = req.query.nom;
var nom = 'Nom complet: ' + req.query.nom + '<br/>';
var organisation = 'Organisation: ' + req.query.organisation + '<br/>';
var courriel = 'Courriel: ' + req.query.courriel + '<br/>';
var telephone = 'Téléphone: ' + req.query.telephone + '<br/>';
var body = 'Message: ' + req.query.message.replace('/(?:'r'n|'r|'n)/g', '<br/>'); // THE ERROR TRIGGERS HERE !!!
var content = nom + organisation + courriel + telephone + body;
console.log(subject);
console.log(content);
mailer.sendMail('aaa@gmail.com', subject, content, next);
});
梅勒var mailer = require('nodemailer');
var EMAIL_ACCOUNT_EMAIL = 'aaa@gmail.com';
var EMAIL_ACCOUNT_PASSWORD = 'aaaa';
var smtpTransport = mailer.createTransport({
service: "Gmail",
auth: {
user: EMAIL_ACCOUNT_EMAIL,
pass: EMAIL_ACCOUNT_PASSWORD
}
});
exports.sendMail = function(toAddress, subject, content, next){
var success = true;
var mailOptions = {
to: toAddress,
subject: subject,
html: content
};
smtpTransport.sendMail(mailOptions, function(error, res){
if(error){
console.log('[ERROR] Message NOT sent: ', error);
success = false;
} else {
console.log('[INFO] Message sent: ' + res.message);
}
next(error, success);
});
}
但是,我在标记行上的node.js控制台上出现错误:
TypeError: Cannot call method 'replace' of undefined
我需要做什么好主意来解决这个问题吗?
出现此错误消息是因为req.query.message未定义。检查变量req.query.message是否存在,这应该可以工作。
相关文章:
- 用嵌套函数和默认函数定义函数
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 控制台返回var不是't定义,但它是
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- jQuery自定义验证比较多个输入的序列
- 查看JS对象的所有键,甚至是getter定义的键
- fetch() 函数未在 Ubuntu Chromium 浏览器上定义
- Replace()产生“;未捕获的类型错误:未定义的不是函数“;当与零一起使用时
- Dojo lang.replace - 如何提供默认值,而不是“未定义”
- Jquery无法调用未定义的方法replace
- 错误:TypeError: Cannot call method 'replace'的定义
- 错误:TypeError: Cannot call method 'replace'的定义
- remote.getGlobal()返回“未定义”;后window.location.replace()
- javascript不能调用'replace'的定义
- 0x800a138f - JavaScript运行时错误:无法获取属性'replace'未定义或空引用的
- TypeError:表达式'的结果;input.replace'[未定义]不是函数