首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我尝试在nodejs中使用twilio从我的前端发送sms时,我收到了这些错误

当我尝试在nodejs中使用twilio从我的前端发送sms时,我收到了这些错误
EN

Stack Overflow用户
提问于 2021-04-26 21:51:03
回答 1查看 68关注 0票数 1

我试图通过一个自定义的API URL从我的前端发送OTP,该URL使用nodejs后端服务器来触发请求。但是我。获取以下错误。

后端代码:

代码语言:javascript
复制
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上,我请求以下参数

代码语言:javascript
复制
http://localhost:4000/api/auth/notification?type=sms&reason=auth&ph=918697836806

我在控制台中遇到的错误

代码语言:javascript
复制
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)

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2021-04-26 22:10:35

res.send()会尝试将你给它的对象串起来。根据twilio docs的说法,你想

代码语言:javascript
复制
 .then((message) => res.send({ message: message.sid, otp: OTP }))

而不是解构从他们的应用程序接口返回的整个message对象。

编辑:试试这个。我认为它做了你想要的。您应该能够从console.log中分辨出您要发回的内容。

代码语言:javascript
复制
 .then((twilioResponse) => {
   message = twilioResponse.sid
   message.otp = OTP
   console.log(message)
   res.send(message)
 } )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67267937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档