【问题标题】:Getting error required parameter opts.to missing获取错误所需的参数 opts.to 丢失
【发布时间】:2019-10-04 09:58:53
【问题描述】:

我正在使用 Twilio SDK 处理我的 Postman 请求。我目前已将凭据存储在 .env 文件中。

exports.send = async (messageBody) => {
  client.messages.create({
    body: messageBody,
    To: twilioTo,
    From: twilioFrom
  }, (err, message) => { 

    message = message + " Goodbye World!";
    if (err) return cb(err);
    cb(null, message);
  });
};

但我得到的不是正常响应:

{
"code": 500,
"message": "Required parameter \"opts.to\" missing.",
"stack": "Error: Required parameter \"opts.to\" missing.\n    at    Function.create (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/node_modules/twilio/lib/rest/api/v2010/account/message.js:95:13)\n    at Object.exports.send (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/src/api/services/twilioSMSVerficationService.js:14:21)\n    at exports.send (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/src/api/controllers/twilio.controller.js:20:42)\n    at Layer.handle [as handle_request] (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/node_modules/express/lib/router/layer.js:95:5)\n    at next (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/node_modules/express/lib/router/route.js:137:13)\n    at /Volumes/BOOTCAMP/knowrify/knowrify-node-backend/src/api/middlewares/auth.js:41:10"
}

【问题讨论】:

  • 找到了在函数范围内无法访问 to 变量的解决方案

标签: node.js express twilio twilio-api


【解决方案1】:

在我意识到我为“to”传递了 undefined 之前,我也遇到了同样的错误。附上示例代码供参考。

const destinationNumber = req.body.number;
const messageConfig = {
                body: 'OTP : ' + otp,
                from: process.env.TWILIO_NUMBER,
                to: destinationNumber
            };
const message = await client.messages.create(messageConfig);
if (message.sid) {
     context.res = {
      body: "OTP sent successfully"
  };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2018-12-16
    相关资源
    最近更新 更多