【问题标题】:Firebase Cloud Function - Reference.update failedFirebase 云功能 - Reference.update 失败
【发布时间】:2018-12-17 13:35:37
【问题描述】:

调用 GCF 时出现以下错误:

错误:Reference.update 失败:指定的第一个参数路径超过了可写入的最大深度 (32) 或对象在属性中包含循环

在网上做了一些挖掘,但找不到相同的问题。在不添加太多信息的情况下,我正在尝试将菜对象添加到实时数据库中的类别对象中。奇怪的是,该功能在前 6 道菜中运行良好,当我尝试添加第 7 道菜时,弹出此错误并且更新方法失败。

完整的错误日志,带有我唯一的属性数据是:

Error: Reference.update failed: First argument path specified exceeds the maximum depth that can be written (32) or object contains a cycle in property 

'users.sIRf7m1uWfa9j0iF6UuuzvdD5TG2.dishcategories.default01.dishes.u3o1p278vriqo3odeslle.categories.0.dishes.irdrl2q7blsi1y7ih3jhh.categories.0.dishes.v8pl7r9llhfp7sqmz7uikk.categories.0.dishes.2ee3ajy6d5vymneewgflze.categories.0.dishes.btdib119nz5cnm6zk5uu4t.categories.0.dishes.4wyu5yqyn2z0bgvcejix9.categories.0.dishes.w1cfcpktym7nkp76p521n.categories.0.createdDate'

    at ValidationPath.checkValid_ (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1035:19)
    at ValidationPath.push (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1015:14)
    at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1478:18
    at Object.forEach (/srv/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)
    at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1462:14)
    at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1479:13
    at Object.forEach (/srv/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)
    at validateFirebaseData (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1462:14)
    at /srv/node_modules/@firebase/database/dist/index.node.cjs.js:1479:13
    at Object.forEach (/srv/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)

这里是 index.ts 中的云函数代码:

// Now we need to check if this dish's categories already exist as user categories.
// If they do, we can add this newly created dish into them.
// If they don't, we can create them with this newly added dish in them.

dish.categories.forEach( async (dishCategory) => {
    const index = objectInArrayByID(dishCategory, userCategories)

    if ( index !== -1 ) {

        return userCategoriesRef.child(`${dishCategory.id}/dishes/${id}`).update(dish) // *** This is the update method producing the error in this case ***
    }

    else {

        await userCategoriesRef.child(`${dishCategory.id}`).update(dishCategory)

        return userCategoriesRef.child(`${dishCategory.id}/dishes/${id}`).update(dish)
    }

 })

任何人都知道这个错误是什么意思,也许我在这里做错了什么?谢谢!

【问题讨论】:

    标签: node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    错误消息非常明确:您尝试写入的数据深度超过32 级别,或者包含循环对象。

    鉴于您共享的代码,后者似乎最有可能:您循环遍历菜肴的每个类别,然后将该菜肴再次写入每个类别。我能想到的最简单的解决方法是将没有类别的菜写到每个类别中:

    dish_without_categories = dish;
    delete dish_without_categories.categories;
    dish.categories.forEach( async (dishCategory) => {
        const index = objectInArrayByID(dishCategory, userCategories)
    
        if ( index !== -1 ) {
    
            return userCategoriesRef.child(`${dishCategory.id}/dishes/${id}`).update(dish_without_categories)
        }
        else {
    
            await userCategoriesRef.child(`${dishCategory.id}`).update(dishCategory)
    
            return userCategoriesRef.child(`${dishCategory.id}/dishes/${id}`).update(dish_without_categories)
        }
    })
    

    【讨论】:

    • 谢谢弗兰克 - 你正中问题:)
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2020-10-19
    • 2021-08-09
    • 2022-09-01
    • 2020-06-09
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多