getUsersinternet: async function () { let res = await wx.cloud.callFunction({ name: 'getUsersinternet', data: { usersID: this.openid, } }) console.log(res.result) this.setData({ productOpenid: [...res.result[0].data], closeopenid: [...res.result[1].data] }) },和const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const _ = db.command exports.main = async (event, context) => { console.log(event) const usersID = event.usersID; let res = await db.collection('usersdatas').where({_id: usersID}).get() const threepartysid = res.data[0].threepartysid // data 是一个数组,取第一个元素的 threepartysid let allid = await db.collection('usersinternet').where({_id: _.in(threepartysid) }).limit(1000).get() const newOpenid = []; const newCloseid = []; for (let i = 0; i < allid.data.length; i++) { // allid 是一个对象,取 data 属性 if (allid.data[i].isEnable === true) { newOpenid.push(allid.data[i]); } if (allid.data[i].isGroupMember === false || allid.data[i].closedate < getToday() ) { newCloseid.push(allid.data[i]); } } const productOpenid = newOpenid.map(item => ( item.threepartyid )); const closeopenid = newCloseid.map(item => ( item.threepartyid )); return [productOpenid,closeopenid]; // 返回数组 }指出这两段代码中的错误,并把正确的代码发给我
时间: 2023-11-28 16:48:08 浏览: 132
第一个代码段中没有明确指出调用的云函数名称,应该改为:
```
let res = await wx.cloud.callFunction({
name: 'getUsersInternet', // 云函数名称
data: {
usersID: this.openid,
}
})
```
第二个代码段中的 `getToday()` 函数没有定义,应该在函数内部定义或者引入。同时,最后返回的应该是一个对象,而不是数组。正确的代码如下:
```
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
console.log(event)
const usersID = event.usersID;
let res = await db.collection('usersdatas').where({_id: usersID}).get()
const threepartysid = res.data[0].threepartysid // data 是一个数组,取第一个元素的 threepartysid
let allid = await db.collection('usersinternet').where({_id: _.in(threepartysid) }).limit(1000).get()
const newOpenid = [];
const newCloseid = [];
for (let i = 0; i < allid.data.length; i++) { // allid 是一个对象,取 data 属性
if (allid.data[i].isEnable === true) {
newOpenid.push(allid.data[i]);
}
if (allid.data[i].isGroupMember === false || allid.data[i].closedate < getToday()) {
newCloseid.push(allid.data[i]);
}
}
const productOpenid = newOpenid.map(item => (
item.threepartyid
));
const closeopenid = newCloseid.map(item => (
item.threepartyid
));
return {productOpenid, closeopenid}; // 返回对象
}
```
阅读全文