uni-app 使用add()时报错 Error: [zhjql]: SYSTEM_ERROR: Cannot read property 'reduce' of undefined

发布于 1周前 作者 gougou168 来自 Uni-App

uni-app 使用add()时报错 Error: [zhjql]: SYSTEM_ERROR: Cannot read property ‘reduce’ of undefined

示例代码:

nupdata = {
    myid: nordernum,
    myname: "生产订单:" + pordernum + " - 工序质检",
    pordernum: pordernum,
    ntype: "工序质检",
    yn: "0"
}
r.nupdata = nupdata;

var norderres = await dbJQL.collection('my-qualityinspection').add(nupdata);

报错:Error: [zhjql]: SYSTEM_ERROR: Cannot read property ‘reduce’ of undefined

我的程序没使用过‘reduce’, 不知道为什么会报这个错

操作步骤:

报错:Error: [zhjql]: SYSTEM_ERROR: Cannot read property ‘reduce’ of undefined

预期结果:

正常返回id

实际结果:

报错:Error: [zhjql]: SYSTEM_ERROR: Cannot read property ‘reduce’ of undefined

bug描述:

使用add()新增记录时报错:Error: [zhjql]: SYSTEM_ERROR: Cannot read property ‘reduce’ of undefined

我的程序没使用过‘reduce’, 不知道为什么会报这个错


3 回复

clientDB日志发一下。


请求入参: { “args”: { “rif”: “mt3getcdata”, “msg”: “小程序读取表信息”, “tablename”: “my-warehouseallocation”, “tablenamearr”: [ “my-warehouseallocation”, “uni-id-users”, “my-gaugeoutfit” ], “field”: [ “_id”, “myid”, “myname”, “company”, “yn”, “dateToString(add(new Date(0),create_date),”%Y-%m-%d","+0800") as create_date_str", “create_date”, “create_uid”, “ntype”, “pordernum”, “printcount”, “finishedproducts” ], “pagesize”: 5, “userkeyarr”: [], “where”: “(_id != null) && (yn == ‘1’) && ntype in [‘普通调拨’]”, “cpage”: 1, “userwhere”: “_id != null”, “clientInfo”: { “PLATFORM”: “mp-weixin”, “OS”: “ios”, “APPID”: “UNIDE05E4F”, “DEVICEID”: “16777364038064375252”, “scene”: 1001, “batteryLevel”: 100, “benchmarkLevel”: -1, “memorySize”: 2048, “safeArea”: { “top”: 47, “left”: 0, “right”: 390, “bottom”: 810, “width”: 390, “height”: 763 }, “screenWidth”: 390, “screenHeight”: 844, “screenTop”: 91, “windowWidth”: 390, “windowHeight”: 753, “statusBarHeight”: 47, “enableDebug”: false, “fontSizeScaleFactor”: 1, “mode”: “default”, “bluetoothEnabled”: true, “locationEnabled”: true, “wifiEnabled”: true, “locationReducedAccuracy”: true, “albumAuthorized”: true, “bluetoothAuthorized”: true, “cameraAuthorized”: true, “locationAuthorized”: true, “microphoneAuthorized”: true, “notificationAuthorized”: true, “notificationAlertAuthorized”: true, “notificationBadgeAuthorized”: true, “notificationSoundAuthorized”: true, “phoneCalendarAuthorized”: true, “deviceOrientation”: “portrait”, “devicePixelRatio”: 3, “deviceId”: “16777364038064375252”, “safeAreaInsets”: { “top”: 47, “left”: 0, “right”: 0, “bottom”: 34 }, “appId”: “UNIDE05E4F”, “appName”: “chaoran_frontend-ERP-sc”, “appVersion”: “1.0.0”, “appVersionCode”: “100”, “appLanguage”: “zh-Hans”, “uniCompileVersion”: “4.06”, “uniRuntimeVersion”: “4.06”, “uniPlatform”: “mp-weixin”, “deviceBrand”: “devtools”, “deviceModel”: “iPhone 12/13 (Pro)”, “deviceType”: “phone”, “osName”: “ios”, “osVersion”: “10.0.1”, “hostVersion”: “8.0.5”, “hostLanguage”: “zh-CN”, “hostName”: “WeChat”, “hostSDKVersion”: “2.33.0”, “hostFontSizeSetting”: 16, “windowTop”: 0, “windowBottom”: 0, “locale”: “zh-Hans”, “LOCALE”: “zh-Hans” }, “uniIdToken”: “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2MzY5Yzc3MDFlNGI2NjAwMDFkNzI1NzYiLCJyb2xlIjpbInByb2R1Y3Rpb25wZXJzb25uZWwiXSwicGVybWlzc2lvbiI6WyJhdXRob3JpdHlwcm9kdWN0aW9uZGVwYXJ0bWVudCJdLCJpYXQiOjE3MTI5OTY2MzMsImV4cCI6MTcxMzI1NTgzM30.9OMNrx0rUcP9gOjrXSACi5EfbOlZFVZsYxCaf52Mx08” }, “requestId”: “ac1cc3041713253498743183913” } [zhjql/ac1cc3041713253498743183913/2ms/DEBUG] event : { rif: ‘mt3getcdata’, msg: ‘小程序读取表信息’, tablename: ‘my-warehouseallocation’, tablenamearr: [ ‘my-warehouseallocation’, ‘uni-id-users’, ‘my-gaugeoutfit’ ], field: [ ‘_id’, ‘myid’, ‘myname’, ‘company’, ‘yn’, ‘dateToString(add(new Date(0),create_date),"%Y-%m-%d","+0800") as create_date_str’, ‘create_date’, ‘create_uid’, ‘ntype’, ‘pordernum’, ‘printcount’, ‘finishedproducts’ ], pagesize: 5, userkeyarr: [], where: ‘(_id != null) && (yn == ‘1’) && ntype in [‘普通调拨’]’, cpage: 1, userwhere: ‘_id != null’, uniIdToken: ‘eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2MzY5Yzc3MDFlNGI2NjAwMDFkNzI1NzYiLCJyb2xlIjpbInByb2R1Y3Rpb25wZXJzb25uZWwiXSwicGVybWlzc2lvbiI6WyJhdXRob3JpdHlwcm9kdWN0aW9uZGVwYXJ0bWVudCJdLCJpYXQiOjE3MTI5OTY2MzMsImV4cCI6MTcxMzI1NTgzM30.9OMNrx0rUcP9gOjrXSACi5EfbOlZFVZsYxCaf52Mx08’ } [zhjql/ac1cc3041713253498743183913/56ms/ERROR] Cannot read property ‘reduce’ of undefined TypeError: Cannot read property ‘reduce’ of undefined at w (/tmp/function/@common_modules/uni-id-common/index.js:1:4496) at <anonymous> at process._tickCallback (internal/process/next_tick.js:189:7) [zhjql/ac1cc3041713253498743183913/57ms/ERROR] Cannot read property ‘reduce’ of undefined Error: Cannot read property ‘reduce’ of undefined at _callCloudFunction (/tmp/function/@common_modules/uni-cloud-jql/index.js:1:6206) at <anonymous> at process._tickCallback (internal/process/next_tick.js:189:7) 请求响应状态: fail

uni-app 中使用 add() 方法时,报错 Error: [zhjql]: SYSTEM_ERROR: Cannot read property 'reduce' of undefined,通常是由于以下几个原因导致的:

1. 参数传递错误

add() 方法可能期望接收一个数组或对象作为参数,但你传递的参数可能是 undefinednull,导致在内部调用 reduce 方法时出错。

解决方法: 确保传递给 add() 方法的参数是一个有效的数组或对象。可以在调用 add() 之前进行参数检查。

if (Array.isArray(yourArray)) {
    add(yourArray);
} else {
    console.error('参数必须是一个数组');
}

2. 方法调用上下文错误

如果你在调用 add() 方法时,this 指向不正确,可能会导致 add() 方法内部的某些依赖对象未正确初始化,从而引发错误。

解决方法: 确保在调用 add() 方法时,this 指向正确。如果是 Vue 组件中的方法,确保使用箭头函数或在调用时绑定正确的上下文。

// 使用箭头函数
const add = () => {
    // 方法实现
};

// 或绑定上下文
this.add = this.add.bind(this);

3. 依赖库或插件问题

如果 add() 方法依赖于某个第三方库或插件,可能是该库或插件未正确初始化或加载,导致 add() 方法内部出现错误。

解决方法: 检查相关的依赖库或插件是否已正确安装和初始化。确保在调用 add() 方法之前,所有依赖都已准备好。

4. 代码逻辑错误

可能是你在 add() 方法内部的代码逻辑中存在错误,比如对未定义的变量进行操作。

解决方法: 检查 add() 方法内部的代码逻辑,确保所有使用的变量都已正确定义和初始化。

function add(array) {
    if (!array || !array.reduce) {
        console.error('参数无效或缺少 reduce 方法');
        return;
    }
    // 继续处理
}

5. 异步操作问题

如果 add() 方法内部涉及异步操作,可能在异步操作完成之前就尝试调用 reduce 方法,导致 undefined 错误。

解决方法: 确保在异步操作完成后再调用 reduce 方法。可以使用 Promiseasync/await 来处理异步操作。

async function add(array) {
    const result = await someAsyncOperation();
    if (result && result.reduce) {
        // 继续处理
    } else {
        console.error('异步操作未返回有效结果');
    }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!