javascript - 如何从 React 中读取和写入 firestore 中的 map 值?

标签 javascript reactjs google-cloud-firestore

我有一个数据库字段,在 firestore 中有一个这样的 map :

坐标:{_01:"铜",_02:"金",_03:"铁"}

我在 firestore 管理面板中看到这个数据库,如下所示: pic

当我尝试使用下面的代码列出项目时

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

我收到:

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

读/写这种 map 的正确方法是什么?

顺便说一句,console.log(data.corords) 给我这样的输出:

{_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: ""

是的,总共有 48 个元素

感谢任何帮助,提前致谢。

最佳答案

map更改为forin

这里是示例代码:

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
   }
}

关于javascript - 如何从 React 中读取和写入 firestore 中的 map 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58779623/

相关文章:

javascript - 等待一组 worker 完成

javascript - 使用自定义 css 在页面中弹出而不覆盖页面的 css

javascript - ReactJS 和 AJAX 未捕获类型错误 : Cannot Read Property 'bugs' of Undefined

java - 运行多个 Firestore 查询并等待所有查询完成

java - 从 Firestore 获取 ArrayList 和文档名称

javascript - JS UNDO DOM 修改

javascript - 仅接受数字和短划线字符的输入文本字段

javascript - React with Redux 中的扩展类

javascript - 特拉维斯/杰斯特 : TypeError: Cannot assign to read only property 'Symbol(Symbol.toStringTag)' of object '#<process>'

python - 如何将数据存储备份转换为 JSON(包括 map 和数组)?