【问题标题】:I'm getting these error while I try to send sms from my frontend using twilio in nodejs当我尝试在 nodejs 中使用 twilio 从前端发送短信时出现这些错误
【发布时间】:2021-04-26 13:51:03
【问题描述】:

我正在尝试通过自定义 API URL 从我的前端发送 OTP,该 URL 使用 nodejs 后端服务器来触发请求。但是我'。收到以下错误。

后端代码:

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require("twilio")(accountSid, authToken);

router.post("/notification", (req, res) => {
  const type = req.query.type || "sms";
  const ph = `+${req.query.ph}` || "";
  const email = req.query.email || "";
  const reason = req.query.reason || "notification";
  const msg = req.body.text || "";

  if (type === "sms" && reason === "auth") {
    const OTP = getRandom(1000, 2000);
    console.log(`OTP is - ${OTP}`);
    client.messages
      .create({
        body: `Hi, Im Sandip. OTP is - ${OTP}`,
        from: "+12012994953",
        to: ph,
      })
      .then((message) => res.send({ ...message, otp: OTP }))
      .catch((err) => console.log(err, "err"));
  }else{ ...some other logic }

我在这个 URL 上请求这些参数

http://localhost:4000/api/auth/notification?type=sms&reason=auth&ph=918697836806

我在控制台中遇到的错误

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Twilio'
    |     property '_accounts' -> object with constructor 'Accounts'
    --- property 'twilio' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\express\lib\response.js:158:21)
    at D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\controller\authController.js:122:30
    at Promise_then_fulfilled (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\q\q.js:766:44)
    at Promise_done_fulfilled (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\q\q.js:835:31)
    at Fulfilled_dispatch [as dispatch] (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\q\q.js:1229:9)
    at Pending_become_eachMessage_task (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\q\q.js:1369:30)
    at RawTask.call (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\asap\asap.js:40:19)
    at flush (D:\Edureka\Projects\Full Stack Projects\E-commerce\backend\node_modules\asap\raw.js:50:29)

提前致谢

【问题讨论】:

    标签: node.js twilio sms twilio-api sms-gateway


    【解决方案1】:

    res.send() 尝试将你给它的对象字符串化。根据twilio docs,你想要

     .then((message) => res.send({ message: message.sid, otp: OTP }))
    

    而不是解构从其 API 返回的整个 message 对象。

    编辑:试试这个。我认为它可以满足您的需求。您应该能够从 console.log 中得知您发回的内容。

     .then((twilioResponse) => {
       message = twilioResponse.sid
       message.otp = OTP
       console.log(message)
       res.send(message)
     } )
    

    【讨论】:

    • 感谢它的工作。但是我不能在 twilio 的响应中添加我的 otp 字段并将整个作为一个对象发送到我的前端。我试过这个.then((message) =&gt; res.send({ message, otp: OTP }))。它发送{message: {.....},top:{....}} 它工作但需要.then((message) =&gt; res.send({ other fields of message, otp: OTP }))
    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2020-06-02
    • 1970-01-01
    • 2021-05-28
    • 2017-02-23
    相关资源
    最近更新 更多