The Walrus
The Walrus

Reputation: 1208

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?

Upvotes: 10

Views: 13589

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599816

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.

Upvotes: 9

Alex Mounir
Alex Mounir

Reputation: 1241

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.

Upvotes: 0

Related Questions