uni-app cloud云路由发送短信bug
uni-app cloud云路由发送短信bug
示例代码:
// 发送短信
async sendingSms() {
const data = this.ctx.data
try {
// 短信额度
const balance = await this.db.collection("natural-resources")
.where({
type: "SMS"
})
.get(); // 统计符合条件的记录数量
const res = await uniCloud.sendSms({
appid: '__UNI__FA6***',
phone: data.phone,
templateId: '320***', // 请替换为自己申请的模板id
data: {
orderNo: data.orderNo,
}
})
console.log("res", res);
// if (balance.data[0].balance >= 10) {
// console.log("111111111");
// // if (res.errCode == 0) {
// // console.log("发送成功");
// // this.db.collection('natural-resources').where({
// // type: "SMS"
// // }).update({
// // 'balance': this.db.command.inc(-10) // 减少余额
// // })
// // }
// }
} catch (e) {
//TODO handle the exception
console.log("短信发送错误", e);
}
}
报错:
[本地调试]短信发送错误 uniCloud-aliyun/cloudfunctions/cloud-router/service/expressDelivery.js:354:11
[本地调试]TypeError: Cannot read properties of undefined (reading 'provider')
操作步骤:
不能同时存在,否则报错TypeError: Cannot read properties of undefined (reading ‘provider’)
去掉其中一个,另一个可以运行
预期结果:
先查询额度,再执行发送短信
实际结果:
发送短信成功
bug描述:
cloud云路由有bug,
我事先查询短信额度,额度满足再执行发送短信。
但是
const balance = await this.db.collection("natural-resources")
.where({
type: "SMS"
})
.get(); // 统计符合条件的记录数量
和
const res = await uniCloud.sendSms({
appid: '__UNI__FA6***',
phone: data.phone,
templateId: '320***', // 请替换为自己申请的模板id
data: {
orderNo: data.orderNo,
}
})
不能同时存在,否则报错TypeError: Cannot read properties of undefined (reading ‘provider’)
去掉其中一个,另一个可以运行
发送短信的代码我是放到 // if (balance.data[0].balance >= 10) {中的,但是出错了,然后我排除错误,就放到了与const balance = await this.db.collection(“natural-resources”)评级
意思是单独调用uniCloud.sendSms不报错?
是的
回复 8***@qq.com: “TypeError: Cannot read properties of undefined (reading ‘provider’)” 这个错误带着错误堆栈截图发出来看看
回复 DCloud_uniCloud_WYQ: 你看看是不是这个
就这样
调用sendingSms的地方怎么写的?
看下是不是哪里漏写了await
回复 DCloud_uniCloud_WYQ: if (result.id) { if (data.message) { this.sendingSms() } return new resultModel(200, “添加成功”) }
回复 DCloud_uniCloud_WYQ: https://mp-05f26e8b-6519-4bf9-b0d4-3f6561d5094a.cdn.bspapp.com/4ff98044a9467fec6f9f10e66540ea42.mp4 演示视频
回复 DCloud_uniCloud_WYQ: 视频有看了吗哥,我好解决问题
回复 8***@qq.com: 需要看代码,注意看你的日志是在云函数结果打印出来之后再打印的发送短信失败,检查下是不是哪里漏写了await导致云函数return之后再执行的发送短信
!
在 uni-app
中使用 uniCloud
进行云函数开发时,可能会遇到发送短信的云路由问题。以下是一些常见的问题和解决方案,帮助你排查和修复 uniCloud
发送短信的 bug
。
1. 确保云函数配置正确
- 检查云函数环境变量:如果你使用的是第三方短信服务(如阿里云、腾讯云等),确保在云函数中正确配置了
SecretId
、SecretKey
等环境变量。 - 依赖安装:如果云函数中需要使用第三方 SDK(如阿里云的
[@alicloud](/user/alicloud)/dysmsapi
),确保在云函数目录下通过npm install
安装了依赖。
2. 检查云函数代码
- 参数校验:确保传入云函数的参数(如手机号、模板 ID、验证码等)是正确的,并且符合短信服务商的要求。
- 错误处理:在云函数中添加错误处理逻辑,捕获并返回详细的错误信息,方便调试。
示例代码:
const dysmsapi = require('[@alicloud](/user/alicloud)/dysmsapi');
const client = new dysmsapi({
accessKeyId: process.env.ALIYUN_ACCESS_KEY_ID,
secretAccessKey: process.env.ALIYUN_ACCESS_KEY_SECRET,
endpoint: 'dysmsapi.aliyuncs.com',
});
exports.main = async (event, context) => {
try {
const { phone, code } = event;
if (!phone || !code) {
throw new Error('参数不完整');
}
const response = await client.sendSms({
PhoneNumbers: phone,
SignName: '你的签名',
TemplateCode: '你的模板ID',
TemplateParam: JSON.stringify({ code }),
});
if (response.Code === 'OK') {
return { success: true, message: '短信发送成功' };
} else {
throw new Error(response.Message);
}
} catch (error) {
return { success: false, message: error.message };
}
};