Firebase云功能-Reference.update失败

时间:2018-12-17 13:35:37

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

调用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)
    }

 })

任何人都知道此错误意味着什么,也许我在这里做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

错误消息非常明显:您正在尝试写入深度超过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)
    }
})