uni-app uni-id版本非3.1.0时短信验证码出现service未定义
uni-app uni-id版本非3.1.0时短信验证码出现service未定义
操作步骤:
```javascript
const uidObj = require('uni-id');
const res = await uidObj.sendSmsCode({
...this.ctx.data,
code: Math.floor(Math.random() * 1000000) + "",
//templateId:"30516"
templateId: uidObj.config['service']['smscode']['templateId'] //发送验证码
});
```
## 预期结果:
```
短信发送成功
```
## 实际结果:
```
code: "INVOKE_FUNCTION_FAILED"
message: "Cannot read property 'service' of undefined"
```
## bug描述:
const uidObj = require('uni-id');
const res = await uidObj.sendSmsCode({
...this.ctx.data,
code: Math.floor(Math.random() * 1000000) + "",
//templateId:"30516"
templateId: uidObj.config['service']['smscode']['templateId'] //发送验证码
});
这是发送手机短信验证码的,当uni-id版本为3.3.31时,出现的错误是Cannot read property 'service' of undefined,给templateId直接赋值是运行是正常的。当uni-id版本改为3.1.0,原代码运行正常,其他版本的uni-id都运行不了
更多关于uni-app uni-id版本非3.1.0时短信验证码出现service未定义的实战教程也可以访问 https://www.itying.com/category-93-b0.html
mark下
更多关于uni-app uni-id版本非3.1.0时短信验证码出现service未定义的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在 uni-app 中使用 uni-id 时,如果遇到短信验证码出现 service 未定义的问题,通常是因为 uni-id 的版本不兼容或配置不正确导致的。以下是一些可能的解决方案:
1. 检查 uni-id 版本
确保你使用的是 uni-id 3.1.0 或更高版本。如果你使用的是旧版本,可能是某些 API 已经发生了变化,导致 service 未定义。
可以通过以下命令更新 uni-id 到最新版本:
npm install uni-id@latest
2. 检查配置文件
确保你的 uni-id 配置文件正确无误。检查 uni-id 配置文件中的 config.json 或 uni-id.config.js,确保短信验证码的相关配置项正确。
例如,确保以下配置项存在:
{
"sms": {
"service": "aliyun", // 或者其他支持的短信服务商
"accessKeyId": "your-access-key-id",
"accessKeySecret": "your-access-key-secret",
"signName": "your-sign-name",
"templateCode": "your-template-code"
}
}
3. 检查代码调用方式
确保你在代码中正确调用了 uni-id 的短信验证码接口。通常,发送短信验证码的代码如下所示:
const uniID = require('uni-id')
uniID.sendSmsCode({
mobile: '13800138000',
type: 'login'
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
确保 mobile 和 type 参数正确传递,并且 uniID 对象已正确初始化。
4. 检查云函数配置
如果你在云函数中使用 uni-id,确保云函数的 package.json 中正确引入了 uni-id,并且在云函数中正确初始化了 uniID 对象。
例如,在云函数的 index.js 中:
const uniID = require('uni-id')
exports.main = async (event, context) => {
const res = await uniID.sendSmsCode({
mobile: event.mobile,
type: event.type
})
return res
}
5. 检查依赖版本
确保你的 uni-id 依赖版本与其他依赖版本兼容。有时,某些依赖版本不兼容可能导致 service 未定义的问题。
6. 查看官方文档
如果以上方法都无法解决问题,建议查看 uni-id 的官方文档,或者到 uni-app 官方社区或论坛中寻求帮助。
7. 调试和日志
你可以在代码中添加调试信息,查看 service 对象是否被正确初始化,或者查看错误日志以获取更多信息。
例如:
console.log(uniID.config) // 查看配置是否正确
console.log(uniID.sms) // 查看短信服务是否正确初始化

