const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const db = cloud.database();
exports.main = async (event, context) => {
const result = await db.collection('goods')
.skip(0)
.limit(10)
.get();
return {
dataList: result?.data,
};
};
代码报错:
Error: cloud.callFunction:fail Error: errCode: -504002 functions execute fail | errMsg: /var/user/fetchGoodsList/index.js:10
const dataList = result?.data;
^
SyntaxError: Unexpected token .
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/user/index.js:7:24) (callId: 1720536929057-0.2917830731559663) (trace: 22:55:29 start->22:55:30 system error (Error: errCode: -504002 functions execute fail | errMsg: /var/user/fetchGoodsList/index.js:10
const dataList = result?.data;
^
SyntaxError: Unexpected token .
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/user/index.js:7:24)), abort)
at R (<anonymous>:1:245532)
at <anonymous>:1:279974(env: Windows,mp,1.06.2405020; lib: 3.4.9)
云函数中不支持可选链操作。把?.去掉。
需要升级云函数nodejs版本
把const dataList = result?.data;其中的?去掉就可以了