我想要用云函数更新云数据库中别人创建的某个记录中某个字段的值,云函数应该如何查找?
发布于 4 年前 作者 egong 4187 次浏览 来自 问答
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async(event, context) => {
  try {
    return await db.collection('forward').where({
      _id:event.index
    }).update({
      data: {
        jopenid: event.jopenid,
        jname: event.jname,
        jphone: event.jphone,
        jmessage: event.jmessage,
        type1: event.type1,
      }
    })
  } catch(e){
    console.error(e)
  }
}

上面是我的云函数的写法。我已经在一个页面的JS层得到了我想要更新的记录的_id是什么,但是请问如何在云函数中根据我所知的_id查找到相应的记录?网上搜不到有用的信息。

onSubmit: function (event) {
    var that= this;
    wx.showModal({
      title: '提示',
      content: '确认提交吗',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '处理中',
            mask: true,
          })
          //这里是点击了确定以后
          wx.cloud.callFunction({
            name:'add',
            data:{
              _id:that.data.index,
              jopenid: getApp().globalData.userInfo.openid,
              jname: event.detail.value.jname,
              jphone: event.detail.value.jphone,
              jmessage: event.detail.value.jmessage,
              type1: true,
            },
            success:function(res){
              wx.redirectTo({
                url: '/pages/delivery/tiaoxuan/tiaoxuan'
              })
              wx.showToast({
                title: '提交成功',
                icon: 'success',
              })
            }
          })
        }
      }
    })
  }

上面是我相应页面的JS的写法。_id是从另一个页面传过来的。请问我如何告诉云函数的where我要找的记录是传过来的_id所携带的值?然后查找到对应的记录之后在更新其中的数据

以下是逻辑介绍:

点击接受订单后补上自己的相关信息点击提交,这时候type1会由false变为true。因此在上一个页面就看不到这个订单了,因为它已被接受

回到顶部