Storing empty arrays in firebase

此生再无相见时 提交于 2021-01-27 06:37:24

问题


I have my firebase app working with React whereby I sign users up and then log there information to the database. I am creating a "dating app" and I would like some way to store empty arrays. e.g, matchers: [] etc

I tried something liket this:

firebase.database().ref('users/' + userId).set({
      id: userId,
      username: name,
      email: email,
      matchers: [],
      likedUsers: [],
      disLikedUsers: []
    });

but then read that firebase doesn't really handle arrays as such and now am not sure how to do this. I want the empty array so then when the user starts using the app they can add to their various arrays.

any ideas?


回答1:


Firebase has always recommended against using arrays. Instead of re-iterating the reasons, I'll point you to the blog post Best Practices: Arrays in Firebase.

But your bigger problem seems to be having empty arrays: the . If a property doesn't have a value, the Firebase Database doesn't store that property. That means that an empty array is not stored in the database and thus not read back.

You'll have to re-create those properties yourself after you read the data back. If you're having problems with this, update your question to show how you read the data.




回答2:


You don't need to create those fields, they are initialized with the first item and the recommended way to indexed collections is to use the push and later to map it to an array.



来源:https://stackoverflow.com/questions/48462093/storing-empty-arrays-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!