0

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.

Faruk
  • 773
  • 1
  • 6
  • 20
  • I'm unclear. What exactly is `data.cordinates` at that point in your code? What are you expecting that code to do? It looks like you're actually trying to just iterate the fields of that object (it's not an array). You wouldn't normally do this with a traditional map operation. – Doug Stevenson Nov 09 '19 at 13:16
  • Please paste the structure of data. i.e console.log(data); – Gangadhar Gandi Nov 09 '19 at 13:20
  • `cordinates` is an `Object` not an `array`. `map` works on array. – Sonu Bamniya Nov 09 '19 at 13:23
  • @SonuBamniya so how can i retrieve an object like this? – Faruk Nov 09 '19 at 13:29
  • @GangadharGandi added console.log – Faruk Nov 09 '19 at 13:29
  • use `for(let i in obj){}` loop instead. – Sonu Bamniya Nov 09 '19 at 13:30
  • It sounds like you want to [iterate through object properties](https://stackoverflow.com/questions/8312459/iterate-through-object-properties). – Doug Stevenson Nov 09 '19 at 13:34
  • @DougStevenson i am trying to read some of the field properties at once as firestore billing is counted per "read" so i didnt make seperate 40 fields alone but also i need to be able to edit these fields easily so didnt make it array. (for example as far as i know i cant edit the third field alone seperately) Each user will read these fields at least once when page loaded, so i want to make the database fields as less as possible or should i switch to firebase as it doesnt count "reads" – Faruk Nov 09 '19 at 14:01
  • All that data is in just one document, which only requires one read to get it all into memory. – Doug Stevenson Nov 09 '19 at 14:02

1 Answers1

1

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
   }
}
Sonu Bamniya
  • 1,095
  • 1
  • 13
  • 29
  • i get Unhandled Rejection (TypeError): cord.hasOwnPorperty is not a function error – Faruk Nov 09 '19 at 13:49
  • Ah! there is typo it should be `cord.hasOwnProperty(i)`. Updated my answer as well. – Sonu Bamniya Nov 09 '19 at 13:51
  • thank you, can you show me how to update one the object field propery also. let say i want to update _03:gold to _03:iron var mineupgrade = db.collection("kullanici").doc(user); mineupgrade.update({ cordinates:[...,_03:"iron"] }); can work? – Faruk Nov 09 '19 at 14:08