firebase云功能 - 参考



调用GCF时我会遇到以下错误:

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

在网上进行了一些挖掘,但找不到同样的问题。在不添加太多信息的情况下,我正在尝试在实时数据库中添加盘式对象中的类别对象。奇怪的是,该功能适用于前6道菜,当我尝试添加第七次时,此错误会弹出并且更新方法失败。

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

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

有人知道这个错误意味着什么,也许我在这里做错了什么?谢谢!

错误消息非常明确:您正在尝试编写比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)
    }
})

最新更新