【发布时间】:2020-02-08 03:43:52
【问题描述】:
有没有办法使用 admin sdk 在实时数据库中的不同字段上执行批处理事务?目前,我正在使用以下内容:
exports.function = functions.https.onCall((data, context) => {
var transactions = new Object();
transactions[0] = admin.database().ref('ref1/')
.transaction(currentCount => {
return (currentCount || 0) + 1;
}, (error, committed, dataSnapshot) => {...})
transactions[1] = admin.database().ref('ref2/')
.transaction(currentCount => {
return (currentCount || 0) + 1;
}, (error, committed, dataSnapshot) => {...})
return admin.database().ref().update(transactions)
// |^| error occurs right above '|^|', but i don't know why, i suspect it may have something to do with transactions object, and if so, what's the proper way to do batched transactions?
.then(result => {...})
.catch(error => {
console.error('error: ' + error)
})
}
但是每次调用这个函数时,虽然事务确实作为一个批处理工作,但会抛出以下错误:
Unhandled error TypeError: obj.hasOwnProperty is not a function
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:541:17)
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1470:9)
at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1487:13
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:542:13)
at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1470:9)
at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1559:9
at each (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:542:13)
at validateFirebaseMergeDataArg (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1557:5)
at Reference.update (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:13695:9)
at admin.firestore.collection.doc.collection.doc.create.then.writeResult (/srv/index.js:447:43)
【问题讨论】:
-
transactions[0]和transactions[1]是什么 - 即 console.log 以查看内容 - 顺便说一下,transactions应该是一个数组而不是一个对象吗? -
@Jaromanda X, transactions[0] 和 transactions[1] 只是记录 [object Promise],我之前使用的是数组,但是在上传函数时收到警告,请改用 object
-
所以它们是承诺...
admin.database().ref().update是否期望一个带有承诺的对象? -
@Jaromanda X 不,文档没有说它专门接受承诺,但它确实接受以子路径作为键和值以及子作为值的对象,但我无法找到批处理事务上的任何内容,仅批处理写入。这是文档显示的有效内容:
usersRef.update({ "alanisawesome/nickname": "Alan The Machine", "gracehop/nickname": "Amazing Grace" });,但这只是正常的批量写入
标签: javascript firebase firebase-realtime-database google-cloud-functions firebase-admin