Nested Query of Cloud Functions in Small Programs

Just using cloud development, I don't know if there are nested query statements in the small program database, so I can only use the most clumsy method to find out the loop and look up again. Okay, next, the question is, have you found out, what kind of value you found? I've been working on this problem for a day. I just started working on the small program side directly, and can get the data. But this seems to be a sequential query, there will be delays, when I have not finished querying the data displayed, resulting in some of my data can not be displayed, because I saw that the data in appData just refreshed the page, the data is not perfect, only to obtain the first data in the sequential query, the latter has not been rendered. Although console.log(res) can display all the data, and after a few minutes back to appData, all the data will be displayed, but my page has been displayed. So I think it's data latency, and I think I have to query all the data before passing in the value. Is this synchronization? So I think I'm going to do it in the cloud function. There's a pit here, for beginners.

Every time I modify the code in the cloud function, I remember to upload and deploy it. Otherwise, it's the same as before. At first, I thought that uploading once was enough (new it automatically helps upload it), and then no matter how I changed it, it didn't show anything.

Let's start with what I'm looking for in nested queries.

Get a list(group) from a group, circle the list(group), get the carid to table carMsg to get the corresponding carMag data, and then get the openid from the previous carMsg data table to get the user. I don't know if the description is clear, group - > carMsg - > user, and finally stitching up the data I want, just return an object, because the return may be a list, so this should be noted. I feel like vomiting blood. My list is a list, if it is single-choice data, it does not need to consider inserting data into the list, directly return the data found directly, and then code:

// Cloud function entry file
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// Cloud function entry function
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext() // The openId and appId obtained here are credible
  //Get the user's openid directly through wxcontext
  const openid = wxContext.OPENID
  //An array of promise s that carry all read operations
  const list = []

  //The next step is to start the query. First, we get the relevant group according to openid. Here we get the list.
  await db.collection('group').where({
    _openid: openid,
    group_reveal: 0
  }).get().then(res => {
    //Loop the queried list and query the corresponding data for each list
    for (let i = 0; i < res.data.length; i++) {
      //per1 is used to define the final object I want, insert the object in the result list, and get the carMsg object according to the corresponding caricd.
      let per1 = db.collection('carMsg').where({
        _id: res.data[i].carMsg
      }).get().then(ce => {
        //Obtain user objects from openid in carMsg objects
        return db.collection('user').where({
          _openid: ce.data[0]._openid
        }).get().then(ue => {
          //Declare objects for the following splicing objects
          const per = {}
          //Stitching together the information you want
          per.groupid = res.data[i]._id
          per.openid = res.data[i]._openid
          per.carid = res.data[i].carMsg
          per.endPoint = ce.data[0].carMsg.endPoint
          per.lowNum = ce.data[0].carMsg.lowNum
          per.estimate = ce.data[0].carMsg.estimate
          per.people = ce.data[0].carMsg.people
          per.nickName = ue.data[0].nickName
          per.avatarUrl = ue.data[0].avatarUrl
          // Return object
          return per
        })
      })
      //Join the list
      list.push(per1)
    }
  })

  return (await Promise.all(list))
}
//Small program end

 onLoad: function(options) {
    var that = this
     wx.cloud.callFunction({
      name:'groupMsg',   //This is the name of the cloud function.
      success:function(e){
        console.log("Success",e)
        that.setData({
          list:e.result  //Set value
        })
      },fail:function(e){
        console.log("fail", e)

      }
    })
  },

 

It always feels like I'm writing complicated. At first, I'm in (the code comment below).

for (let i = 0; i < res.data.length; i++) {

let per1 = db.collection('carMsg').where({

//Here _id: res.data[0].carMsg, my zero does not change back to i, so you can find the data, I don't know why.

 

_id: res.data[i].carMsg

}).get().then(ce => {

 

Novice into the pit, more advice.

Keywords: Database SDK

Added by james_andrews on Tue, 14 May 2019 14:10:23 +0300