10

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?

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. Commented Jan 26, 2018 at 13:22
  • @Kim cheers Kim! this looks good to me Commented Jan 26, 2018 at 13:28

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is super problematic if you want to empty those arrays. Say he has some matches but the user wants to delete them, he can't actually empty the object because firebase will not accept an update with empty array property. So he'll have to build a remove action altogether.
I'm just JSON.stringifying the array now and parse them when fetched from the db.
@aviya.developer You can update an empty array... it essentially just deletes the entry since, as Frank said, Firebase doesn't store empty arrays.
0

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.

2 Comments

but when I create a user I want to store these as easier to read
it doesn't store empty arrays, u can set it at start to true but can you tell me for what reason u need it at start?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.