【问题标题】:Customizing a functions success response / node.js自定义函数成功响应 / node.js
【发布时间】:2021-01-21 23:05:43
【问题描述】:

我将这个 node.js 代码作为 IBM Cloud 函数获取,让我的 Watson Assistant 用户通过电子邮件向我发送提醒。效果很好。

var nodemailer = require('nodemailer');

let smtpConfig = {
    host: 'hosthere', // you can also use smtp.gmail.com
    port: porthere,
    secure: false, // use TLS
    auth: {
        user: 'emailhere', 
        pass: 'passwordhere'
    }
};

function main(params) {
    return new Promise(function (resolve, reject) {
        let response = {
            code: 200,
            msg: 'E-mail was sent successfully!'
        };

        if (!params.reminder) {
            response.msg = "Error: Reminder was not provided.";
            response.code = 400;
        }
        else if (!params.email) {
            response.msg = "Error: Destination e-mail was not provided.";
            response.code = 400;
        }
        else if (!params.conversation_id) {
            response.msg = "Error: Conversation id was not provided.";
            response.code = 400;
        }
        if (response.code != 200) {
            reject(response);
        }

        console.log(`Validation was successful, preparing to send email...`);

        sendEmail(params, function (email_response) {
            response.msg = email_response['msg'];
            response.code = email_response['code'];
            response.reason = email_response['reason'];
            console.log(`Email delivery response: (${email_response['code']}) ${response.msg}`);
            resolve(response);
        });

    });
}

function sendEmail(params, callback) {

    let transporter = nodemailer.createTransport(smtpConfig);
    let mailOptions = {
        from: `Watson Assistent Message <${smtpConfig.auth.user}>`,
        to: params.email,
        subject: `REMINDER: ${params.reminder}`,
        text: `Do it!`
    };
    transporter.sendMail(mailOptions, function (error, info) {

        let email_response = {
            code: 200,
            msg: 'Email was sent successfully',
            reason: 'Success'
        };

        if (error) {
            email_response.msg = 'Error';
            email_response.code = 500;
            email_response.reason = error;
        }
        else {
            email_response.msg = info.response;
            email_response.code = 200;
            email_response.reason = info.response;
        }
        callback(email_response);
    });
}

我只需要在请求成功后自定义“结果”中显示的响应。所以我可以打印给用户类似“电子邮件发送成功”(如果它真的成功的话)

当调用该函数时,它给了我这个:

Results:
{
  "code": 200,
  "msg": "250 Requested mail action okay, completed: id=1N0Fxf-1lzPHJ457n-00xO6a",
  "reason": "250 Requested mail action okay, completed: id=1N0Fxf-1lzPHJ457n-00xO6a"
}
Logs:
[
  "2021-01-20T23:11:38.706021Z    stdout: Validation was successful, preparing to send email...",
  "2021-01-20T23:11:39.117946Z    stdout: Email delivery response: (200) 250 Requested mail action okay, completed: id=1N0Fxf-1lzPHJ457n-00xO6a"
]

问题是,打印到“msg”的整个答案在代码中没有定义。实际上,代码中有一个成功请求的定义答案,只是没有打印出来。

这是为什么呢?我怎么能改变它?

有了错误响应,一切正常。代码中定义的答案会打印在“结果”中:

Results:
{
  "error": {
    "code": 400,
    "msg": "Error: Destination e-mail was not provided."
  }
}
Logs:
[
  "2021-01-21T09:47:14.531598Z    stdout: Validation was successful, preparing to send email..."
]

成功消息有什么问题?

【问题讨论】:

  • 你在哪里/如何打电话给main
  • 测试功能并获得结果:和日志:如图所示,我可以在 IBM 云功能 Web 界面中使用参数调用。需要的参数是:“email”:“anyemailaddress”,“reminder”:“just text”,“conversation_id”:“1234–1234–1234”@eol 在实际应用中,这些参数将通过 Webhook 从 Watson 助手发送到功能 - 也可以正常工作 - 只想自定义成功发送时显示的响应

标签: javascript node.js json


【解决方案1】:

当你定义时:

msg: 'Email was sent successfully', in email_response

它只是被if/else 覆盖。

如果您希望 电子邮件已成功发送 消息(或者如果不正常),请执行以下操作:

callback(error ? {
  code: 500,
  msg: 'Error',
  reason: error
} : {
  code: 200,
  msg: 'Success',
  reason: info.response.includes('okey') ? 'Email was sent successfully' : info.response 
})

【讨论】:

    【解决方案2】:

    你可以这样做来得到一个概括的信息,

    function sendEmail(params, callback) {
    
        let transporter = nodemailer.createTransport(smtpConfig);
        let mailOptions = {
            from: `Watson Assistent Message <${smtpConfig.auth.user}>`,
            to: params.email,
            subject: `REMINDER: ${params.reminder}`,
            text: `Do it!`
        };
        transporter.sendMail(mailOptions, function (error, info) {
    
            // Sets the response object, changing it to a generalized message.
            let email_response = 'Email was sent successfully';
    
            if (error) {
                email_response = 'Email was not sent successfully';
            }
    
            callback(email_response);
        });
    }
    

    像这样调用sendEmail,

    sendEmail(params, function (email_response) {
                response = email_response;
                resolve(response);
            });
    

    【讨论】:

    • 感谢您的回答@hasan 不幸的是,这将导致结果:只是空的。 {} 只是括号,没有其他显示。 :/ 我会说它与主要功能有关,而不是与 sendEmail...
    • 或者我是否还必须更改主函数中的某些内容才能使您的代码正常工作?原因日志:显示此“2021-01-21T14:27:27.582610Z 标准输出:电子邮件传递响应:(未定义)未定义”,
    • 请检查,我认为这是您发布的代码所能做的全部。
    猜你喜欢
    • 2018-07-31
    • 2023-02-22
    • 1970-01-01
    • 2019-07-01
    • 2021-08-10
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多