错误:TypeError:无法调用未定义的方法“替换" [英] err: TypeError: Cannot call method 'replace' of undefined

查看:98
本文介绍了错误:TypeError:无法调用未定义的方法“替换"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过POST请求发送表单:

I'm trying to send form via a POST request this way:

客户端

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控制台上的标记行出现错误:

but, i'm getting an error on the node.js console on the marked line:

TypeError:无法调用未定义的方法"replace"

TypeError: Cannot call method 'replace' of undefined

有什么好主意需要我解决吗?

Any brilliant idea need I to do to fix that, please?

推荐答案

您有此错误消息,因为req.query.message未定义.检查变量req.query.message是否存在,并且应该可以正常工作.

You have this error message because req.query.message is undefined. Check that the variable req.query.message exists and this should work.

这篇关于错误:TypeError:无法调用未定义的方法“替换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆