uni-app Vue3小程序Promise请求报错u找不到
uni-app Vue3小程序Promise请求报错u找不到
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| PC | Windows | CLI |
| 操作系统 | win10 | |
| 工具 | 最新 | |
| 基础库 | 2.16.1 | |
| CLI | 2.0.0-alpha-31520210315001 |
操作步骤:
- 打开对应版本的vue3项目+ts
- 尝试去构建一个请求,promise
预期结果:
正确请求到数据无报错
实际结果:
报错:TypeError: u is not a function
bug描述:
后端是云函数
报错:TypeError: u is not a function
例子:
export async function getQuestionAreaList(): Promise<ActionResult> {
// 获取题目专区列表
return new Promise((resolve) => {
db.collection("questionArea")
.where("deleteDate == ''")
.get()
.then((res) => {
const { success, result } = res;
resolve({
success,
data: result.data,
});
})
.catch((err: { message: string }) => {
console.log(err);
uni.showToast({
title: err.message,
icon: "none",
});
});
});
}
这个方法走了catch,打印了err
根据源码位置,找到了如下线索:
return {
PLATFORM: "mp-weixin",
OS: d,
APPID: h.appid,
LOCALE: u(),
DEVICEID: e,
CLIENT_SDK_VERSION: "1.0.0"
};
更多关于uni-app Vue3小程序Promise请求报错u找不到的实战教程也可以访问 https://www.itying.com/category-93-b0.html
加我微信 npm_install_s
更多关于uni-app Vue3小程序Promise请求报错u找不到的实战教程也可以访问 https://www.itying.com/category-93-b0.html
调用云函数出现的这个问题,我分析了一下源码;
vendor.js:
a = _e2.t,
c = _e2.setLocale,
u = _e2.getLocale;
console.log(u); // 此时u是undefined,对象合并出现了问题
var e2 = Object(dcloudio_uni_i18n__WEBPACK_IMPORTED_MODULE_1[“initVueI18n”])({
“zh-Hans”: {
“uniCloud.init.paramRequired”: “缺少参数:{param}”,
“uniCloud.uploadFile.fileError”: “filePath应为File对象”
},
“zh-Hant”: {
“uniCloud.init.paramRequired”: “缺少参数:{param}”,
“uniCloud.uploadFile.fileError”: “filePath应为File对象”
},
en: {
“uniCloud.init.paramRequired”: “{param} required”,
“uniCloud.uploadFile.fileError”: “filePath should be instance of File”
},
fr: {
“uniCloud.init.paramRequired”: “{param} required”,
“uniCloud.uploadFile.fileError”: “filePath should be instance of File”
},
es: {
“uniCloud.init.paramRequired”: “{param} required”,
“uniCloud.uploadFile.fileError”: “filePath should be instance of File”
}
}, “zh-Hans”),
a = _e2.t,
c = _e2.setLocale,
u = _e2.getLocale;
initVueI18n这个函数没有返回get方法;
return {
t(key, values) {
return t(key, values);
},
setLocale(newLocale) {
return i18n.setLocale(newLocale);
},
mixin: {
beforeCreate() {
const unwatch = i18n.watchLocale(() => {
this.$forceUpdate();
});
this.$once(‘hook:beforeDestroy’, function () {
unwatch();
});
},
methods: {
$$t(key, values) {
return t(key, values);
},
},
},
};
已解决,升级了@dcloudio/uni-i18n
这个错误通常是由于uni-app在V3版本下国际化配置问题导致的。错误信息中u()对应的是获取语言环境的方法,但在当前上下文中未正确定义。
在你的代码中,问题可能出现在以下方面:
- 国际化配置缺失:检查
main.ts或应用入口文件是否缺少createI18n配置 - 平台差异处理:小程序环境下需要特殊处理国际化模块
建议修改方案:
// 在main.ts中确保有正确的i18n配置
import { createSSRApp } from 'vue'
import App from './App.vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
同时检查uni-app配置文件中是否包含必要的国际化设置。如果不需要国际化功能,可以在manifest.json中配置关闭相关功能:
{
"vueVersion": "3",
"h5": {
"devServer": {
"port": 8080
}
}
}
对于请求函数,建议简化Promise封装,直接使用async/await:
export async function getQuestionAreaList(): Promise<ActionResult> {
try {
const res = await db.collection("questionArea")
.where("deleteDate == ''")
.get()
return {
success: res.success,
data: res.result.data
}
} catch (err: any) {
console.log(err)
uni.showToast({
title: err.message,
icon: "none"
})
throw err
}
}

