Collection.watch 在删除和更新数据时不触发
//app.js onLaunch 中注册
initWatchGoodsCollection(){
let that = this;
const db = wx.cloud.database({
env: 'commodity-display-ckxt7',
});
const watcher = db.collection('goods').watch({
onChange: snapshot=> {
console.log(111);
console.log('snapshot', snapshot)
//初始化时会触发一次
if (snapshot.type!="init"){
that.initGoodsList();
}
},
onError: err=> {
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
}
}