注意 data内值格式应为: name:this.data.name,前端
使用setdata只是为了对前端作一遍更新web
const db=wx.cloud.database() const _=db.commond var that=this db.collection("bank").add({ data: { databaseValueName:VarInYourProgram, }, success:function(res){ wx.showToast({ title:"添加成功", duration:2000 }) that.setData({ databaseValueName:VarInYourProgram, }) }, fail:function(res){ wx.showToast({ title:"添加失败", duration:2000 }) } })
goodId是数据库待删除记录的_id值数据库
const db=wx.cloud.database() const _=db.commond var that=this db.collection("bank").doc(goodId).remove({ success:function(res){ wx.showToast({ title:"删除成功", duration:2000 }) }, fail:function(res){ wx.showToast({ title:"删除失败", duration:2000 }) } })
若是前端使用<form wx:for("{{datas}}")>,可使用前端返回的index来查找_id值后端
例如:异步
前端:(goods为data保存的数据库的值,goodName是一个属性)svg
<form bindsubmit="action" wx:for="{{goods}}" wx:for-index="index" wx:for-item="item" wx:key="{{item.goodName}}"> <input hidden='true' name='index' value={{index}}> <button form-type='submit'> submit</button> </form>
后端:
这样就得到了_idthis
action:function(e){ var index=e.detail.value["index"] var id=this.data.goods[index]._id }
若是须要将查找到的数据添加到data中,请先var that=this以后使用that.setdata({})code
const db=wx.cloud.database() const _=db.commond //详情见云开发手册command eq,lt,gt,in,and等 //此处查询theAttrYouSearch中等于aaa的记录 db.collection("bank").where({ theAttrYouSearch : _.eq("aaa") }).get({ //若成功获取,异步操做注意异常 success: res=>{ //打印记录中第一条里goodName属性 console.log(res.data[0].goodName) } })
const db=wx.cloud.database() const _=db.commond //获取goodId的方法同删除操做 db.collection("bank").doc(goodId).update({ //只修改goodNum属性 data: { goodNum: num }, success:res=>{ }, fail: res=>{ } )}