最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Storing empty arrays in firebase - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jan 26, 2018 at 14:28 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Jan 26, 2018 at 13:16 The WalrusThe Walrus 1,2087 gold badges31 silver badges49 bronze badges 2
  • You can store arrays in the FireStore database if it's okay with your project requirements. I suppose you also could store the arrays as subcollections in the FireStore. – Kim Commented Jan 26, 2018 at 13:22
  • @Kim cheers Kim! this looks good to me – The Walrus Commented Jan 26, 2018 at 13:28
Add a ment  | 

2 Answers 2

Reset to default 9

Firebase has always remended 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.

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

发布评论

评论列表(0)

  1. 暂无评论