【发布时间】:2019-08-20 23:56:07
【问题描述】:
我要做什么
我目前正在为数据库设置 Firebase 函数,为此我使用的是 TypeScript。 我正在尝试从我的数据库中获取一个节点。该节点是一个 Map(即 String 的 Map,Java 中的 bool)。问题是从我的数据库中获取节点“测试”后,我试图遍历其成员,但出现错误
错误
TypeError:agenda.forEach 不是函数
在对象。 (\firecast\functions\lib\index.js:32:16)
在Generator.next()
在完成时 (\firecast\functions\lib\index.js:4:58)
在 process._tickCallback (internal/process/next_tick.js:68:7)
我的设置
这是我设置数据库的方式:(对不起,我知道为什么图像嵌入不起作用,我会放链接)
图片链接:https://pasteboard.co/I7KogY2.png
这是我的完整功能:
export const getTest=
functions.https.onRequest(async (request,response)=>{
try{
const GlobalCalendar= await admin.firestore().doc("Calendar/GlobalCalendar").get()
const agenda=GlobalCalendar.data()!.test
const hours: any[]=[]
agenda.forEach((weekday: any) => {
hours.push(weekday)
});
response.send(agenda);
}
catch(error){
console.log(error)
response.status(500).send(error)
}
})
我尝试修复它
如果我注释掉
// 常量小时数:any[]=[]
//agenda.forEach((weekday: any) => {
// hours.push(工作日)
// });
它将成功发送地图“议程”,我得到以下信息:
{ “四”:真, “一”:是的, “三”:真, “二”:真 }
结论
所以我知道我从数据库中正确获取了节点“测试”,但问题在于迭代这个对象。似乎无论“议程”的类型是什么,它都没有“forEach”功能。我按照 Firebase youtube 视频中的说明进行操作,他们做了同样的工作,但与我的不同,他们的工作完美。
视频链接:https://www.youtube.com/watch?v=Jr7pDZ1RAUg&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=6
我的预期结果是能够遍历变量“议程”中的元素,这是我在图像中的节点“测试”。
非常感谢您的宝贵时间!
【问题讨论】:
-
那么,
agenda到底是什么?听起来好像不是你想的那样。 -
感谢您的快速回复@DougStevenson。 'agenda' 是我试图从文档'GlobalCalendar' 或换句话说,'Calendar/GlobalCalendar/test' 中保存字段'test' 的变量(也许我的数据库的图像在这里会有所帮助)
标签: node.js typescript firebase google-cloud-firestore google-cloud-functions