个人案例
- 云开发“拼团”功能,数据查询?
我设计的拼团功能数据结构,团长(Leader)和团员(Member)都生成了独立的订单号(在同一个集合里‘groupOrder’)。 团员在下单时会关联了团长的订单(ID)。 我现在想把每个团长及团员的订单筛选出来放到一个新数组里面。 [{ 团长A: '', 团员[{ 团员A: '', }, { 团员B: '', }] }, { 团长B: '', 团员[{ 团员A: '', }, { 团员C: '', }] }] 云函数,查询‘Leader._id’,并通过‘addFields()函数’新建了一个数组‘memberOrder’,再通过查询到的‘Leader._id’,继续查询符合条件的‘member’订单,并把查询结果push到新数组‘memberOrder’,但返回结果null exports.main = async (event, context) => { const wxContext = cloud.getWXContext() const req_type = event.req_type switch (req_type) { case "Member": return memberCheck(); default: { return } } function memberCheck() { const memberOrder = [] return dbRes.match({ shopID: event.shopID, orderType: 'Leader' }).addFields({ memberOrder: memberOrder }).end() .then( res => { console.log(res); const leaderOrder = res.data for (let i = 0; i < leaderOrder.length; i++) { const orderID = leaderOrder[i]._id; return db.collection('groupOrder').where({ orderID: orderID }).get() .then( res => { const member = res.data for (let j = 0; j < member.length; j++) { const tempObj = member[j]; memberOrder.push(tempObj) } return res }) } return res }).catch( err => { console.log(err); }) } }
2020-08-10 - 云开发从集合直接删除数据后为什么提示数据加载失败?
开始的时候Collection.limit()设置为20,删除多条数据后再加载就提示加载失败,Collection.limit()改为18,就可以正常加载。但是删除的数据不止2条。 js 文件 _fetchTributeHeroList: function () { wx.showLoading() wx.cloud.callFunction({ name: 'fetchTributeHeroList', data: { type: "TributeHero", startIndex: taskList.length, count: 20, }, }).then(res => { dbAllCount = res.result.dbAllCount console.log(dbAllCount) var list = res.result.list if (list.length > 0) { if (taskList.length == 0) { taskList = list } else { taskList = taskList.concat(list) } this.setData({ tributeHeroList: taskList, }) console.log("获取致敬英雄列表:", this.data.tributeHeroList) } wx.hideLoading() }).catch(err => { console.log(err) wx.hideLoading() wx.showToast({ title: '数据加载失败', }) }) }, 云函数 const wxContext = cloud.getWXContext() var list = [] var start = 0 var count = 1000 if (event.startIndex) start = event.startIndex if (event.count) count = event.count var dbRes = db.collection('PublishTask').aggregate() // 如果传入的类型,就按类型获取 if (event.type) { dbRes.match({ type: event.type }) } // 如果 isMyself 有值,并且是 true,就获取当前用户自己的发布 if (event.isMyself) { dbRes.match({ userId: wxContext.OPENID }) } dbRes.sort({ createAt: -1, // -1 代表降序排列(从大到小) }) dbRes.skip(start) dbRes.limit(count) dbRes.lookup({ from: 'User', localField: 'userId', foreignField: '_id', as: 'userInfo', }) var taskList = await dbRes.end() list = taskList.list
2020-07-26 - 云函数 Command.elemMatch 查询结果为什么是空?
想通过 adminInfo字段查询,对象内{adminOpenID: wxContext.OPENID,}的值是否一直。但查询结果为空。 [图片] [图片] exports.main = async (event, context) => { const wxContext = cloud.getWXContext() const _ = db.command return db.collection('smallShop').where({ adminInfo: _.all([ _.elemMatch({ adminOpenID: wxContext.OPENID, }) ]), }).get() }
2020-07-20 - 通过button按钮转发,为什么没有返回值?
onShareAppMessage: function (res) { if (res.from === 'button') { } return { title: '改革开放前的历史证明,邻里关系好,有助于家庭稳定!', path: '/pages/community/propertyAdimn/repairman/repairman?orderDetail=' + JSON.stringify(this.data.orderDetail), imageUrl: '../../../../images/150-8.jpg', success: function (res) { console.log(res) }, fail: function (res) { console.log(res) } } }
2020-07-14 - 云函数使用switch()函数报错?
云函数使用switch()函数报错 [图片] exports.main = async (event, context) => { const wxContext = cloud.getWXContext() switch (req_type) { case 'Add': { return await db.collection('RepairOrder').add({ data: { date: event.date, time: event.time, communityId: event.communityId, orderType: event.orderType, orderProgress: event.orderProgress, projectName: event.projectName, projectType: event.projectType, projectPrice: event.projectPrice, descValue: event.descValue, address: event.address, contactInfo: event.contactInfo, status: event.status, userId: wxContext.OPENID, userInfo: event.userInfo, createAt: new Date() } }).then( res => { console.log(res) return res }) } case 'Recall' : { return await db.collection('RepairOrder').where({ _id: event._id }) .update({ data: { status: event.status, deleteAt: new Date() } }).then( res => { console.log(res) return res }) } case 'Repairman' : { return await db.collection('RepairOrder').where({ _id: event._id }) .update({ data: { orderProgress: _.push(event.progressItem), } }).then( res => { console.log(res) return res }) } default: { return } } js文件 wx.cloud.callFunction({ name: 'add_RepairOrder', data: { req_type: 'Add', date: this.data.date, time: this.data.time, communityId: this.data.communityId, orderType: this.data.orderType, orderProgress: this.data.orderProgress, projectName: this.data.projectName, projectType: this.data.projectType, projectPrice: this.data.projectPrice, descValue: this.data.descValue, address: this.data.address, contactInfo: this.data.contactInfo, status: this.data.status, userInfo: app.globalData.userInfo } }).then( res => { console.log(res) // 强制刷新上一页 var pages = getCurrentPages(); // 获取页面栈 var prevPage = pages[pages.length - 2]; // 上一个页面 prevPage.setData({ update: true }) wx.hideLoading() wx.navigateBack({ delta: 1 }) })
2020-07-13 - 云函数提交数据时,有空数据该如何处理?
请问提交数据时出现空数据该如何处理? [图片] formSubmit: function (e) { if (this.data.serviceTypes == 'Free') { const freeTemp = { name: this.data.serviceValue, desc: this.data.descValue, } this.setData({ freeObj: freeTemp, }) } else { const chargeTemp = { price: this.data.priceValue, name: this.data.serviceValue, desc: this.data.descValue, } this.setData({ chargeObj: chargeTemp, }) } this.setData({ inputValue: '', }) wx.showLoading() this._send() wx.hideLoading() }, // 提交数据到服务器 _send() { wx.cloud.callFunction({ name: 'add_Project', data: { _id: this.data.adminInfo._id, freeObj: this.data.freeObj, chargeObj: this.data.chargeObj } }).then(res => { wx.hideLoading() console.log(res) }) }, [图片]
2020-07-08 - 开发工具无法登录?
1.03.2006091/mac,突然提示重新登录。退出再打开就这样了,网络正常 [图片]
2020-07-08 - 为什么云函数调用成功,返回结果null?
云函数 exports.main = async (event, context) => { const wxContext = cloud.getWXContext() try { return await db.collection('Community').doc('event._id') .update({ data: { managers: event.managers } }) } catch (err) { console.log(err) } } [图片]
2020-07-04 - 给globalData赋值失败?
app.js(console.log("用户身份", this.globalData.propertyAdmin))已经成功获取到新的值,为什么赋值不成功。 globalData: { userInfo: null, openid: null, propertyAdmin: 0, //(runnerType)0用户,66管理员 propertyInfo: null, //跑腿员个人信息 }, onLaunch: function (options) { console.log(this.globalData.propertyAdmin) if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力') } else { wx.cloud.init({ env: 'msy-150cm-ixpt', // 开发环境 traceUser: true, }) } this.getOpenid(); this._getUserInfo(); this._getPropertyAdmin() }, //获取用户身份 _getPropertyAdmin: function (openid) { //请求自己后台获取用户身份 wx.cloud.callFunction({ name: "fetchAdmin", data: { action: "getAdmin" } }).then(res => { console.log(res) if (res.result && res.result.data && res.result.data[0]) { let admin = res.result.data[0]; this.globalData.propertyInfo = admin.adminType; this.globalData.propertyAdmin = admin.propertyAdmin; console.log("用户身份", this.globalData.propertyAdmin) } else { this.globalData.propertyAdmin = 0; } }).catch(error => {}) }, [图片]
2020-07-03 - 云函数add()成功后返回空数据?
云函数add()成功后返回空数据? 云函数 [图片] js函数 [图片] 返回结果 [图片]
2020-07-02