自定义函数成功响应/ node.js

塞布

我获得了此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..."
]

成功消息有什么问题?

劳伦斯·谢罗(Lawrence Cherone)

定义时:

msg: 'Email was sent successfully',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 
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Node.JS中向响应对象添加自定义函数

来自分类Dev

登录后的Node.js自定义成功重定向页面

来自分类Dev

自定义响应消息,可验证Node js中的所有必需参数

来自分类Dev

带有Mongoose自定义JSON响应的Node.js MongoDb

来自分类Dev

node.js:如何以自定义格式HTTP获取和解码/编码响应

来自分类Dev

使用 Node.js SDK 创建自定义响应对象

来自分类Dev

Node.js REPL具有自定义eval函数的有趣行为

来自分类Dev

如何在node.js中创建自定义异步函数?

来自分类Dev

带有自定义eval函数的Node.js REPL有趣行为

来自分类Dev

使用函数对象的 Node.js 自定义中间件

来自分类Dev

node.js http-proxy自定义路由

来自分类Dev

Openshift上的自定义node.js版本

来自分类Dev

从硬盘安装自定义Node.js模块

来自分类Dev

node.js http-proxy自定义路由

来自分类Dev

使用Node.js做自定义头像

来自分类Dev

Node.js中Cloud Firestore的自定义对象在快照中运行.data不是函数错误

来自分类Dev

Node Js链接响应

来自分类Dev

Node js,函数不等待响应

来自分类Dev

Node js,函数执行

来自分类Dev

使用 winston.js 和 node.js 自定义消息格式

来自分类Dev

node.js自定义事件:无法调用未定义的方法“ emit”

来自分类Dev

node.js自定义事件:无法调用未定义的方法“ emit”

来自分类Dev

Node.js:在另一个自定义类中引用自定义类

来自分类Dev

获取Node.js函数定义

来自分类Dev

获取Node.js函数定义

来自分类Dev

JS 函数的自定义 valueOf

来自分类Dev

如何修改Node JS响应

来自分类Dev

Node js swagger 响应说明

来自分类Dev

带有自定义sessionID错误的node.js Express 4会话