Micro letter applet cloud development database data can not be modified others

In the micro-channel applet cloud development greatly facilitated our server free ride, free background function, the official storage and database functions for us, of course, these features are also flawed.

Today, to record what database permissions issue
Here Insert Picture Description
official permission to our database rights in four small program ends

  1. All users read, create families who only read and write (for user comments, users public information)
  2. Only the creator can read and write (for personal settings, personal information, etc.)
  3. All user-readable (for product information, etc.)
  4. All users can not read and write (for background data flow)

More than four cases, we are not able to be used for all scenarios, for example, user A gift presented to the user B, user A gift decrease, increase B presents the user, in this case, it is necessary to modify A user's data, but also to modify the user data B, a, we can modify the user, but the user B should not limit the rights, we can not be modified , it is clear that these four rights does not meet the current demand, again without us how to do it?

After reading official documents, we mentioned earlier permissions to the database in the small end of the program, we can 云函数be resolved.

Create a cloud function

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

cloud.init()

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  return await db.collection('集合名').doc(event.id)
  .update({
    data: {
      // B 用户自增一
      evaluate: _.inc(1)
    }
  })
}

Create a cloud function also need to install dependencies

In the new cloud function, right-click Terminal to open -> cmd, install dependencies
npm install --production
after dependent successful installation files will appear inside package-lock.jsona file.

After completing the installation and deployment of cloud upload function completes the creation of cloud function

Cloud function call

wx.cloud.callFunction({
 name: 'update',
  data: {
    id: 数据集合id
  },
  success: res => {
    console.log(res)
    wx.showToast({
      title: '赠送成功',
    })
  },
  fail: err => {
    wx.showToast({
      icon: 'none',
      title: '赠送失败',
    })
    console.error('[云函数] [sum] 调用失败:', err)
  }
})

ok, I'm done

Published 25 original articles · won praise 20 · views 4942

Guess you like

Origin blog.csdn.net/qq_39157944/article/details/104426300