0

enter image description here

So I am getting Data from a firestore document that comes out to look like this. {xgvppy7455eeyjllj62yuf: {…}, sorxzerz1ynvy1urmllkdk: {…}, K3N7U2ThQCPVUl4b4rWAfVJiP2d2: {…}} The encrypted stuff is just the ID I use to identify the fields in the document, now they have objects in them that look like this.

enter image description here

K3N7U2ThQCPVUl4b4rWAfVJiP2d2:
  balance: "208843"
  description: "Rainy Day"
  name: "Rainy Day Pod"

sorxzerz1ynvy1urmllkdk:
  balance: "214352"
  description: "Sammiched"
  name: "Sammich"

How can I access just the value of the description key in both objects?

Aplogies, heres my code for getting the document data.

var docRef = db.collection("pods").doc("K3N7U2ThQCPVUl4b4rWAfVJiP2d2");


docRef.get().then(function(doc) {
if (doc.exists) {
    console.log("Document data:", doc.data());
  • I recommend editing the question to include 1) the code you have now 2) the data in the database using a screenshot, so we can more clearly see the data you're working with, 3) what you've tried that doesn't work the way you expect. – Doug Stevenson Aug 31 '20 at 19:41
  • @DougStevenson Thanks! Just did! – Emmanuel Terngu Aug 31 '20 at 19:55
  • 1
    Please post the raw JSON, I don't know why a user with 180k rep would ask you to post a screenshot of your data. That's not the correct way to ask a question. – I wrestled a bear once. Aug 31 '20 at 20:29
  • @Iwrestledabearonce The screenshot is helpful so that we can see that the data is actually a single document and not a query full of documents. The question was not very clear on this point, since it didn't include code (and still doesn't include code). Usually, though, people provide desktop screenshots rather than photos. – Doug Stevenson Aug 31 '20 at 20:47
  • {sorxzerz1ynvy1urmllkdk: {…}, K3N7U2ThQCPVUl4b4rWAfVJiP2d2: {…}, xgvppy7455eeyjllj62yuf: {…}}K3N7U2ThQCPVUl4b4rWAfVJiP2d2: {name: "Rainy Day Pod", balance: "208843", description: "Rainy Day"}sorxzerz1ynvy1urmllkdk: {description: "Sammiched", name: "Sammich", balance: "214352"}xgvppy7455eeyjllj62yuf: {description: "Sammiched", name: "Sammich", balance: "214352"}__proto__: Object Thanks! @Iwrestledabearonce. – Emmanuel Terngu Aug 31 '20 at 21:01
  • @DougStevenson. Apologies and put in the excerpt of the code, I am getting the entire document from firestore and want to get the description from each object. – Emmanuel Terngu Aug 31 '20 at 21:05

2 Answers2

0

You could iterate the whole object, regardless of the key (which is hashed). To iterate an object you could do:

const obj = {... some data with hashed keys ...}

Object.keys(obj).forEach((key) => {
    const value = obj[key];
    const description = value.description;
    // ... do something with the description;
});
paroxyzm
  • 1,422
  • 2
  • 13
  • 29
0

doc.data() returns a plain JavaScript object whose properties and values match the fields of the document. It looks like each of your random strings is a separate field in the document, which means they would be the properties of the returned object.

You can use any of the techniques from this other question to iterate the properties of a JavaScript object. For example:

const data = doc.data()
for (field in data) {
    console.log(field)       // logs the random field name
    console.log(data[field)) // logs the object data of that field
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441