Retrieving an Array from Firebase

How to retrieve an array data inside an array from firebase database: Android

Since children is a list, you're probably looking for:

public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.child("children").getChildren()) {
System.out.println(childSnapshot.getValue(String.class));
}
}

Note that the Firebase documentation recommends against using arrays for such lists, such as in this blog post. Instead it recommends using push() to populate lists.

How to retrieve an array from Firebase Firestore and output it onto an HTML table

I think you are mixing up the documents and the fields.

Making the assumption user IDs (i.e. uid) are unique, the query on db.collection("users").where("uid", "==", uid) will return only one document.

Then, in this unique document, you have a field named reserved_items which is of type Array. More precisely it is an Array of Objects. So you need to loop over the Array elements, not on the QuerySnapshot.

So the following should do the trick:

  let query = db.collection('users').where('uid', '==', uid);

query
.get()
.then((querySnapshot) => {
const queryDocumentSnapshot = querySnapshot.docs[0]; //There is only one doc in the QuerySnapshot!!

const reservedItemsArray = queryDocumentSnapshot.data()
.reserved_items;

reservedItemsArray.forEach((obj, index) => {
let row = `<tr><td><a onclick="barcode=${obj.Barcode}; myFunction(index)">${obj.Title}</a></td></tr>`;
table.innerHTML += row;
});

// ...
})
.catch((err) => {
console.log(`Error: ${err}`);
});

It's not 100% clear to me:

  • What you want to display for the link text... I've used the Title with obj.Title but you may adapt.
  • What you want to do with myFunction(this). You should probably use the index, e.g. like myFunction(index), which indicates the position of the element in the Array.
  • Why you limit to 100. It's up to you to adapt the above code if you only want to display 100 lines in the table. But again, if you need to limit the number of elements to be displayed, this limit shall apply to the elements of the Array, not to the query.

How to get array from Real-Time Database in Firebase

Something like this should do the trick:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("products");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Product> products = new ArrayList<Product>();
for (DataSnapshot productSnapshot: dataSnapshot.getChildren()) {
Product product = productSnapshot.getValue(Product.class);
products.add(product);
}
System.out.println(products);
}

@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}

A few things to note:

  • This code throws away the key of each product (p1 and p2 in your JSON). You may need these keys if you later want to modify or delete the product, so should consider storing them in your Product class. Also see: Is there a way to store Key in class which I cast from Firebase object?
  • You really owe it to yourself and to us to spend some time in the documentation reading up on listeners and queries.

How to retrieve data one by one from an array in the Firestore

No, there is not. When you read a document, you always get the entire contents of the document. There is no way to limit that.

Your only workaround is to put each array item in a separate document, and query for only those documents you need.

How to get Array type data in Firebase in Kotlin?

Firebase has a toObject method that can be used to turn your document into a custom object.

db.collection("Comments")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
val comment = document.toObject<Comment>()
}
}

The Comment data class should also define default values. So, it should be like...

data class Comment(
val uid: String = "",
val comment: String = "",
@ServerTimeStamp val stamp: Date? = null
)


Related Topics



Leave a reply



Submit