Problems encountered: Want to use the small program cloud development real-time push data ability to complete database update in the cloud function, cannot monitor in the client

Cause: Data update can only be monitored when it is completed on the client, but not when it is placed in the cloud function

Problem code:

Cloud functions cannot be updated:

const cloud = require('wx-server-sdk'Cloud.init () const db = cloud.database() // cloud function exports.main = async (event, event) context) => { const wxContext = cloud.getWXContext() await db.collection('messages').add({
        data: {
       
        groupId: 'message',}}); }Copy the code

Solution:

methods: {
  async like(e) {

    const doc = {
      _id: `${Math.random()}_${Date.now()}`,
      groupId: 'message',
      sendTime: new Date(),
      sendTimeTS: Date.now(), // fallback
    }

    const db = wx.cloud.database()

    await db.collection('messages').add({
      data: doc,
    })
  }
}
Copy the code