Nodejs、MongoDB、Rest API 我在這里使用 twilio 服務在 MongoDB 資料庫中發送電話 otp SMS,SMS 正在發送并且沒有錯誤,但 otps 沒有保存在資料庫中。請幫助我如何將 otps 保存在資料庫中。
try {
const { phone } = req.body;
const user = await User.findOne({ phone: phone });
if (!user) {
return res.status(404).json({ success: false, message: 'Phone not found.'
})
}
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
client.verify.services.create({friendlyName: `My First Verify Service` })
.then(service => console.log(service.sid));
client.verify.services(process.env.TWILIO_SERVICES_KEY)
.verifications
.create({ to: user.phone, channel: `sms ` })
.then(verification => {
console.log(verification.status)
});
client.verify.services(process.env.TWILIO_SERVICES_KEY)
.verificationChecks
.create({to: user.phone, code: `${user.phoneOtp}`})
.then(verification_check => console.log(verification_check.status));
await user.save();
return res.status(200).json({ success: true, message: 'OTP sended to your
registered phone number.' });
} catch (err) {
return res.status(200).json({ success: false, message: 'OTP not send try
again' });
}
uj5u.com熱心網友回復:
嘗試將await user.save();發送短信的服務放入您的服務中。你可以把它放在里面.then。
uj5u.com熱心網友回復:
從我在這里看到的情況來看,您還沒有將 OTP 保存到資料庫中。在發送 OTP 添加之后
user.phoneOtp = code;和 await user.save();繼續之前。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/362656.html
