我试图通过一个自定义的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)提前感谢
发布于 2021-04-26 22:10:35
res.send()会尝试将你给它的对象串起来。根据twilio docs的说法,你想
.then((message) => res.send({ message: message.sid, otp: OTP }))而不是解构从他们的应用程序接口返回的整个message对象。
编辑:试试这个。我认为它做了你想要的。您应该能够从console.log中分辨出您要发回的内容。
.then((twilioResponse) => {
message = twilioResponse.sid
message.otp = OTP
console.log(message)
res.send(message)
} )https://stackoverflow.com/questions/67267937
复制相似问题