unicloud 云函数“上传运行”报错:Cannot read properties of undefined (reading 'test'),本地正常
在函数中调用IoTDA 时报错误:
使用下面日志放到初始化方法中:
// ============ SDK 诊断日志 ============ console.log('======== SDK 诊断开始 ========'); // 1. 检测 SDK 版本 try { const corePackage = require('@huaweicloud/huaweicloud-sdk-core/package.json'); const iotdaPackage = require('@huaweicloud/huaweicloud-sdk-iotda/package.json'); console.log('✅ SDK 版本检测:'); console.log(' - core 版本:', corePackage.version); console.log(' - iotda 版本:', iotdaPackage.version); } catch (e) { console.log('❌ 无法读取 package.json:', e.message); } // 2. 检测模块导出 const huaweiCore = require('@huaweicloud/huaweicloud-sdk-core'); console.log('✅ 模块导出检测:'); console.log(' - BasicCredentials 类型:', typeof huaweiCore.BasicCredentials); console.log(' - BasicCredentials 静态方法:', Object.getOwnPropertyNames(huaweiCore.BasicCredentials).filter(m => typeof huaweiCore.BasicCredentials[m] === 'function') ); console.log(' - getDefaultDerivedPredicate 存在:', 'getDefaultDerivedPredicate' in huaweiCore.BasicCredentials); console.log(' - getDefaultDerivedPredicate 值:', huaweiCore.BasicCredentials.getDefaultDerivedPredicate); // 3. 检测实例方法 const testCreds = new huaweiCore.BasicCredentials(); console.log('✅ 实例方法检测:'); console.log(' - 实例方法列表:', Object.getOwnPropertyNames(Object.getPrototypeOf(testCreds)).filter(m => typeof testCreds[m] === 'function') ); console.log(' - isDerivedAuth 存在:', 'isDerivedAuth' in testCreds); console.log(' - derivedPredicate 初始值:', testCreds.derivedPredicate); // 4. 检测 Node.js 版本 console.log('✅ 运行环境检测:'); console.log(' - Node.js 版本:', process.version); console.log(' - 平台:', process.platform); console.log(' - 架构:', process.arch); console.log('======== SDK 诊断结束 ========'); // ==================================
打日志发现是云上版本是3.1.185 本地是 3.1.182 。
于是把本地升到3.1.185 也报同样的错。
所以就彻底降到:3.1.182 版本。
# 1. 清除 npm 缓存 npm cache clean --force # 2. 清除 npx 缓存 npx clear-npx-cache # 3. 删除本地 node_modules(如果还在) cd F:\WorkGitHub\wejia\source\ui\wejia\uniCloud-alipay\cloudfunctions\iotda-proxy rd /s /q node_modules del package-lock.json del yarn.lock 2>nul #注意:不要使用 ^ 或 ~ 前缀! npm install @huaweicloud/huaweicloud-sdk-core@3.1.182 @huaweicloud/huaweicloud-sdk-iotda@3.1.182 --save-exact npm i
然后:
1. 打开 HBuilderX
2. 打开 "uniCloud" 面板(左侧或视图菜单中)
3. 展开 "cloudfunctions" → 找到 "iotda-proxy"
4. 右键点击 "iotda-proxy"
5. 选择 "删除云函数" 或 "管理云函数" → "删除"
重新上传运行就已经对了

浙公网安备 33010602011771号