Faruk
Faruk

Reputation: 853

How can i read and write values from a map in firestore from react?

I have a database field which has a map like this in firestore:

cordinates:{_01:"copper",_02:"gold",_03:"iron"}

i see this database in firestore admin panel like this: pic

when i try to list items with the code below

data.cordinates.map((item, i)=>
                  console.log(i+" - "+item)
                );

i receive:

Unhandled Rejection (TypeError): data.cordinates.map is not a function

what is the correct way to read/write this kind of map?

btw console.log(data.cordinates) give me output like this:

{_01: "copper", _02: "gold", _03: "iron", _04: "", _05: "", …}
_01: "copper"
_02: "gold"
_03: "iron"
_04: ""
_05: ""
_06: ""
_07: ""
_08: ""
_10: ""
_11: ""
_12: ""
_13: ""
_14: ""
_15: ""
_16: ""
_17: ""
_18: ""
_20: ""
_21: ""
_22: ""
_23: ""
_24: ""
_25: ""
_26: ""
_27: ""
_28: ""
_30: ""
_31: ""
_32: ""
_33: ""
_34: ""
_35: ""
_36: ""
_37: ""
_38: ""
_40: ""
_41: ""
_42: ""
_43: ""
_44: ""
_45: ""
_46: ""
_47: ""
_48: ""

yes there are 48 elements in total

Any help is aprreciated, thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

Sonu Bamniya
Sonu Bamniya

Reputation: 1115

Change map to forin

Here is example code:

const cord = data.cordinates || {} // to handle undefined `data.cordinates` case
for(let i in cord){
   if(cord.hasOwnProperty(i)){
     const item = cord[i];
     console.log(i+" - "+item);
     // do your other stuff here
   }
}

Upvotes: 1

Related Questions