Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing empty arrays in firebase

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?

like image 253
The Walrus Avatar asked Jan 26 '18 13:01

The Walrus


1 Answers

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.

like image 166
Frank van Puffelen Avatar answered Oct 04 '22 22:10

Frank van Puffelen