Firebase云功能-未处理的错误错误:Update() [英] Firebase cloud function - Unhandled error Error: Update()

查看:65
本文介绍了Firebase云功能-未处理的错误错误:Update()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人尝试用批处理进行多次更新吗?这是我的功能:

Does anyone have a similar problem when trying to do multiple updates with batch? Here is my function:

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 () {
            // ...
        });


    });

我的功能有问题吗?我这样做就像在批处理写入的文档示例中一样.我收到此错误:

Is something wrong with my function? I am doing this just like in documentation example for batched writes. I get this error:

未处理的错误错误: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在export.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(native)\ 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"

Unhandled error Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument \"dataOrField\" is not a valid Firestore document. Couldn't serialize object of type \"NumericIncrementTransform\" (found in field followingNum). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the \"new\" operator).\n at WriteBatch.update (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/write-batch.js:367:23)\n at exports.unfollowRemoveUser.functions.https.onCall (/user_code/lib/index.js:131:11)\n at /user_code/node_modules/firebase-functions/lib/providers/https.js:330:32\n at next (native)\n at /user_code/node_modules/firebase-functions/lib/providers/https.js:28:71\n at __awaiter (/user_code/node_modules/firebase-functions/lib/providers/https.js:24:12)\n at func (/user_code/node_modules/firebase-functions/lib/providers/https.js:294:32)\n at corsHandler (/user_code/node_modules/firebase-functions/lib/providers/https.js:350:44)\n at cors (/user_code/node_modules/firebase-functions/node_modules/cors/lib/index.js:188:7)\n at /user_code/node_modules/firebase-functions/node_modules/cors/lib/index.js:224:17"

我搜索并尝试了所有可以找到的但无法解决此问题的方法,甚至尝试对文档使用简单的更新,但出现相同的错误.

I searched and tried everything I could find but couldn't solve this problem, I even tried to use simple update to the document but I get the same error.

编辑如果要让批处理仅在所有操作都可以成功执行的情况下执行,该怎么办.例子:如果我对确实存在的文档进行前两次更新,它们将以-1更新这些值,然后对不存在的文档进行两次删除操作,这不会对我的函数造成错误(值将被-1更新并删除将不会执行任何操作)

EDIT What should I do when I want batch to execute only if all operations can be executed successfully. Example: If I have first two updates on documents that do exist, they will update those values by -1 and then two delete operation on documents that do not exists, this won't cause an error to my function (values will be updated by -1 and delete won't do anything)

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();

但是,如果我首先对不存在的文档进行两次删除操作,则整个功能将失败

But if I first have two delete operations on documents that do not exists, whole function will fail

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();

我认为附加到批处理的所有内容只有在所有操作都能成功执行后才会执行

I thought that anything that is attached to batch would execute only if all operations can be successfully executed

推荐答案

我在 FieldValue 上遇到了类似的问题.正如道格所说,您需要导入" FieldValue 才能使用 increment()方法.

I have had a similar problem with FieldValue. As Doug mentioned you need to 'import' the FieldValue to use the increment() method.

您可以内联.

batch.update(user1DocRef, {followingNum : admin.firestore.FieldValue.increment(-1)});
batch.update(user2DocRef, {followersNum : admin.firestore.FieldValue.increment(-1)});

或者您可以使用更短的名称定义一个常量.

Or you could define a constant with a shorter name.

admin.initializeApp();
const db = admin.firestore();
const firestore = 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 : firestore.FieldValue.increment(-1)});
    batch.update(user2DocRef, {followersNum : firestore.FieldValue.increment(-1)});

    // Commit the batch
    return batch.commit().then(function () {
        // ...
    });


});

这篇关于Firebase云功能-未处理的错误错误:Update()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆