小程序云函数update更新修改数据库内容报错: query.update is only available in server SDK / API

thirdScriptError
errCode: -1  | errMsg: query.update is only available in server SDK / API;at "pages/result" page lifeCycleMethod onLoad function
Error: errCode: -1  | errMsg: query.update is only available in server SDK / API

百度翻译:

第三个脚本错误

errcode:-1;errmsg:query.update仅在服务器sdk/api中可用;在“pages/result”页lifecyclemethod onload函数中

错误:errcode:-1 errmsg:query.update仅在服务器sdk/api中可用

报错代码 (代码可不看) :

let db = wx.cloud.database()
const _ = db.command 
let userCollection = db.collection("user")

userCollection.where({"_openid":"xxxxx"}).update({
   data:{
       score: _.push([99])
   }
})

解决办法=>放到云函数中 (代码可不看):

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()//链接数据库
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  const { OPENID } = cloud.getWXContext()
  const userCollection = db.collection('user')//选取数据库
  const thisID = userCollection.where({'_openid':OPENID})

  return thisID.get().then(res=>{
    if(res.data.length===0){//如果不存在 - 新增  res.data[0].score
      return userCollection.add({
        data:{
          [`score${event.index}`]: [event.score],
          '_openid': OPENID
        }
      })
    }else{//如果表存在字段 - 更新
      return thisID.update({
        data:{
          [`score${event.index}`]: _.push([event.score])
        }
      })
    }
  })
  
}

原因: 修改数据库内容需要管理员,或者调用服务器设置好的云函数来进行修改.用户是不能修改的.

参考: https://www.jianshu.com/p/07bf2e061cd5

发布了127 篇原创文章 · 获赞 150 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/qq_40259641/article/details/102738973
今日推荐