Reputation: 25
some null data gets included in my database when I store it on an array on my react native app even though it does not have any null entries on the firebase realtime database.
this is the firebase realtime database in my console.
This is my database get function:
const GetDatabaseData = () =>
{
if(DataRendered == false)
{
database()
.ref('Items/ItemInventory')
.on('value', Snapshot => {
var Data = []
Snapshot.forEach((child) => {
Data.push({
Item_code: child.child('Item_code').val(),
Item_name: child.child('Item_name').val(),
Item_qty: child.child('Item_qty').val()
})
})
console.log(Data)
//setItemData(Data)
})
}
else
{
console.log("Data Rendered")
}
}
it's my first time using firebase on react native so any help is appreciated.
Upvotes: 0
Views: 930
Reputation: 1
start index array with 0 if start with index[1] return array is [null,data[1],data[2]]
Upvotes: 0
Reputation: 599816
This is the expected behavior. When Firebase sees a number of sequential, numerical keys it assumed that those are an array, and it fill missing indexes with null
value.
To prevent this array coercion, use a short alphanumeric prefix on your keys, e.g. key1
, key2
, etc.
For more on this, see Best Practices: Arrays in Firebase.
Upvotes: 4