const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const _ = db.command const col = db.collection('products') // 云函数入口函数 exports.main = async (event, context) => { console.log(event) let res = await db.collection('config').doc('prices').get() let p = res.data let ps const queryList = event.queryList; reslist = await col.where({ _id: _.in(queryList)}) .lookup({ // 连接查询 from: 'usersproducts', localField: '_id', foreignField: 'products_id', as: 'data' }) .limit(event.limit) .get() const list = reslist.data if (list.length === 0) { console.log('查询结果为空'); return; } const order = event.queryList.filter(id => list.some(item => item._id === id)); list.sort((a, b) => { const indexA = order.indexOf(a._id); const indexB = order.indexOf(b._id); return indexA - indexB; }); console.log(list); ps = list console.log(ps)这段代码存在报错,报错信息是:Error: cloud.callFunction:fail Error: errCode: -504002 functions execute fail | errMsg: TypeError: col.where(...).lookup is not a function。你能修复这个问题,并把正确的代码发给我
时间: 2023-06-26 08:09:47 浏览: 60
这个错误的原因是 `col` 变量没有定义 `where` 和 `lookup` 方法。可以修改代码如下:
```
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command
const col = db.collection('products')
// 云函数入口函数
exports.main = async (event, context) => {
console.log(event)
let res = await db.collection('config').doc('prices').get()
let p = res.data
let ps
const queryList = event.queryList;
let reslist = await col.where({ _id: _.in(queryList)})
.lookup({ // 连接查询
from: 'usersproducts',
localField: '_id',
foreignField: 'products_id',
as: 'data'
})
.limit(event.limit)
.get()
const list = reslist.data
if (list.length === 0) {
console.log('查询结果为空');
return;
}
const order = event.queryList.filter(id => list.some(item => item._id === id));
list.sort((a, b) => {
const indexA = order.indexOf(a._id);
const indexB = order.indexOf(b._id);
return indexA - indexB;
});
console.log(list);
ps = list
console.log(ps)
return ps
}
```
这里的修改是将 `reslist` 的定义从 `let` 改为 `var`,同时给 `reslist` 变量加上 `let` 或 `var` 关键字,避免变量重复定义。另外,也添加了 `exports.main` 函数的返回值为 `ps`。