收藏
回答

Collection.watch 在删除和更新数据时不触发

框架类型 问题类型 终端类型 AppID 基础库版本
小程序 Bug 微信安卓客户端 wxcc1513b2af3644ff 2.8.3
//app.js onLaunch 中注册
initWatchGoodsCollection(){
    let that = this;
    const db = wx.cloud.database({
      env'commodity-display-ckxt7',
    });
    const watcher = db.collection('goods').watch({
      onChangesnapshot=> {
        console.log(111);
        console.log('snapshot', snapshot)
        //初始化时会触发一次
        if (snapshot.type!="init"){
          that.initGoodsList();
        }
      },
      onErrorerr=> {
        console.error('the watch closed because of error', err)
      },
  
    })
  },

更新和删除在均在云函数中调用

// 云函数入口文件
const cloud = require('wx-server-sdk')


// 初始化 cloud
cloud.init({
  // API 调用都保持和云函数当前所在环境一致
  env: cloud.DYNAMIC_CURRENT_ENV
})


//初始化数据库
const db = cloud.database();
//初始化商品表
const GoodsCollection = db.collection("goods");


// 云函数入口函数
exports.main = async (event, context) => {
  let status = "";
  let errMsg = "";


  const goodsObj = event.goodsObj;
  let goods = await GoodsCollection.where({
    uuid: goodsObj["uuid"],
  }).update({
    data: { 
      name: goodsObj["name"],
      kind: goodsObj["kind"],
      price: goodsObj["price"],
      origin_price: goodsObj["origin_price"],
      description: goodsObj["description"],
      deliver_price: goodsObj["deliver_price"],
      departure_place: goodsObj["departure_place"],
    }
  }).then(res => {
    status = "success";
    errMsg = "";
  }).catch(e => {
    status = "failed";
    errMsg = e;
  })


  return {
    status: status,
    errMsg: errMsg
  }
}


回答关注问题邀请回答
收藏

2 个回答

  • showms
    showms
    2021-07-25

    要修改一下云数据库集合权限设置,改为所有用户可读

    2021-07-25
    有用
    回复 1
  • 耿霄
    耿霄
    2020-03-20

    没用过,非常抱歉

    2020-03-20
    有用
    回复
登录 后发表内容
问题标签