使用云函数来无视权限更新数据库,将数据库中score1的值自增,但是使用db.command.inc()无效
云函数如下:
// runDB云函数入口文件
const cloud = require(‘wx-server-sdk’)
cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
const targetDB = db.collection(event.db)
try {
if (event.type == “insert”) {
return await targetDB.add({
data: event.data
})
}
if (event.type == “update”) {
return await targetDB.doc(event.indexKey).update({
data: event.data
})
}
if (event.type == “delete”) {
return await targetDB.where(event.condition).remove()
}
if (event.type == “get”) {
return await targetDB.where(event.condition)
.skip(20 * event.skip)
.limit(event.limit)
.get()
}
} catch (e) {
console.error(e)
}
}
函数调用如下
wx.cloud.callFunction({
name: ‘runDB’,
data: {
type: “update”, //指定操作是update
db: “vote_counter”, //指定操作的数据表
indexKye: “a5bb3551-c57a-4eda-97d6-6b6df24aa48f”,//制定操作的数据记录
data: { //指定update的数据
score1: _.inc(1)
}
},
success: res => {
console.log(’[云函数] [updatetDB2] 已修改Subjcts信息’ )
},
fail: err => {
console.error(’[云函数] [updatetDB2] 修改Subject失败’, err)
}
})
__如果把参数改成: __score:具体数值 的形式,数据库中的score1会改变值,但是如上述所示参数运行却不会
__
__
日志内容:
日志内容 Request ID: a78ac08b-6d7f-11e9-8491-525400e8849e
执行时间: 45.56ms内存使用: 272.00 KB
返回结果
null
日志
START RequestId: a78ac08b-6d7f-11e9-8491-525400e8849e
Event RequestId: a78ac08b-6d7f-11e9-8491-525400e8849e
2019-05-03T08:44:25.122Z { Error: errCode: -502001 database request fail | errMsg: [FailedOperation.Insert] multiple write errors: [{write errors: [{Cannot create field 'operands' in element {score1: 1.0}}]}, {<nil>}];
at new CloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6166:28)
at Object.returnAsCloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6218:16)
at Object.checkError (/var/user/node_modules/wx-server-sdk/index.js:1484:23)
at DocumentReference.<anonymous> (/var/user/node_modules/wx-server-sdk/index.js:1417:41)
at step (/var/user/node_modules/tslib/tslib.js:133:27)
at Object.next (/var/user/node_modules/tslib/tslib.js:114:57)
at fulfilled (/var/user/node_modules/tslib/tslib.js:104:62)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
errCode: -502001,
errMsg: '[FailedOperation.Insert] multiple write errors: [{write errors: [{Cannot create field \'operands\' in element {score1: 1.0}}]}, {<nil>}]; ' }