【发布时间】:2019-09-12 22:08:20
【问题描述】:
在尝试使用批处理进行多次更新时,是否有人遇到过类似的问题? 这是我的功能:
admin.initializeApp();
const db = admin.firestore();
export const unfollowRemoveUser = functions.https.onCall((data, context) => {
const user1id = data.user1
const user2id = data.user2
const user1DocRef = db.collection('users').doc(user1id)
const user2DocRef = db.collection('users').doc(user2id)
const batch = db.batch();
batch.update(user1DocRef, {followingNum : FieldValue.increment(-1)});
batch.update(user2DocRef, {followersNum : FieldValue.increment(-1)});
// Commit the batch
return batch.commit().then(function () {
// ...
});
});
我的功能有问题吗?我正在这样做,就像在批处理写入的文档示例中一样。 我收到此错误:
未处理的错误错误:Update() 需要单个 JavaScript 对象或字段/值对的交替列表,可以后跟可选的前置条件。参数“dataOrField”的值不是有效的 Firestore 文档。无法序列化类型为 \"NumericIncrementTransform\" 的对象(在字段 followingNum 中找到)。 Firestore 不支持具有自定义原型的 JavaScript 对象(即通过 \"new\" 运算符创建的对象)。\n 在 WriteBatch.update (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore /build/src/write-batch.js:367:23)\n 在exports.unfollowRemoveUser.functions.https.onCall (/user_code/lib/index.js:131:11)\n 在/user_code/node_modules/firebase -functions/lib/providers/https.js:330:32\n 在下一个(本机)\n 在 /user_code/node_modules/firebase-functions/lib/providers/https.js:28:71\n 在 __awaiter (/ user_code/node_modules/firebase-functions/lib/providers/https.js:24:12)\n 在 func (/user_code/node_modules/firebase-functions/lib/providers/https.js:294:32)\n 在 corsHandler (/user_code/node_modules/firebase-functions/lib/providers/https.js:350:44)\n 在 cors (/user_code/node_modules/firebase-functions/node_modules/cors/lib/index.js:188:7) \n 在 /user_code/node_modules/firebase-functions/node_modules/cors/lib/index.js:224:17"
我搜索并尝试了我能找到的所有东西,但无法解决这个问题,我什至尝试对文档使用简单的更新,但我得到了同样的错误。
编辑 只有在所有操作都可以成功执行的情况下,我希望批处理执行时应该怎么做。例子: 如果我对确实存在的文档进行了前两次更新,它们会将这些值更新为 -1,然后对不存在的文档进行两次删除操作,这不会导致我的函数出错(值将更新为 -1并且删除不会做任何事情)
const batch = db.batch();
batch.update(user1DocRef, {followingNum : admin.firestore.FieldValue.increment(-1)});
batch.update(user2DocRef, {followersNum : admin.firestore.FieldValue.increment(-1)});
batch.delete(user1FollowingDocRef); //this doesn't exist
batch.delete(user2FollowersDocRef); //this doesn't exist
return batch.commit();
但是如果我首先对不存在的文档进行两次删除操作,整个函数都会失败
const batch = db.batch();
batch.delete(user1FollowingDocRef); //this doesn't exist
batch.delete(user2FollowersDocRef); //this doesn't exist
batch.update(user1DocRef, {followingNum : admin.firestore.FieldValue.increment(-1)});
batch.update(user2DocRef, {followersNum : admin.firestore.FieldValue.increment(-1)});
return batch.commit();
我认为任何附加到批处理的东西只有在所有操作都可以成功执行的情况下才会执行
【问题讨论】:
-
我希望这能奏效。您是如何导入 FieldValue 符号的?我在这里看不到。
-
@DougStevenson import * as functions from 'firebase-functions';从'firebase-admin'导入*作为管理员;从“@google-cloud/firestore”导入 { FieldValue };也在 package.json "dependencies": { "@google-cloud/firestore": "^1.2.0", "firebase-admin": "^7.3.0", "firebase-functions": "^2.3.0 " },
-
如果您有第二个问题,请单独发布。
-
太棒了!我错误地导入了
"@google-cloud/firestore",然后从中调用了FieldValue.increment(),正确的方法是直接从admin.firestore.FieldValue.increment()导入 -
是的,我遇到了与@Guilherme 相同的问题。在我更新我的 firestore 版本并更新到节点 10 之前,它工作正常。
标签: typescript firebase google-cloud-firestore google-cloud-functions