Javascript 无法循环通过node.js中的嵌套对象
我正试图循环通过一个嵌套对象并将数据保存到我的cloud firestore数据库,但它不起作用 这是我从API调用中检索到的对象的结构Javascript 无法循环通过node.js中的嵌套对象,javascript,node.js,firebase,Javascript,Node.js,Firebase,我正试图循环通过一个嵌套对象并将数据保存到我的cloud firestore数据库,但它不起作用 这是我从API调用中检索到的对象的结构 { "count": 133, "filters": { "dateFrom": "2021-04-12", "dateTo": "2021-04-22", "perm
{
"count": 133,
"filters": {
"dateFrom": "2021-04-12",
"dateTo": "2021-04-22",
"permission": "TIER_ONE"
},
"matches": [
{
"id": 304061,
"competition": {
"id": 2021,
"name": "Premier League",
"area": {
"name": "England",
"code": "ENG",
"ensignUrl": "https://upload.wikimedia.org/wikipedia/en/a/ae/Flag_of_the_United_Kingdom.svg"
}
},
"season": {
"id": 619,
"startDate": "2020-09-12",
"endDate": "2021-05-23",
"currentMatchday": 31,
"winner": null
},
"utcDate": "2021-04-12T17:00:00Z",
"status": "SCHEDULED",
"matchday": 31,
"stage": "REGULAR_SEASON",
"group": "Regular Season",
"lastUpdated": "2021-04-12T16:51:35Z",
"odds": {
"homeWin": 3.43,
"draw": 3.31,
"awayWin": 2.15
},
"score": {
"winner": null,
"duration": "REGULAR",
"fullTime": {
"homeTeam": null,
"awayTeam": null
},
"halfTime": {
"homeTeam": null,
"awayTeam": null
},
"extraTime": {
"homeTeam": null,
"awayTeam": null
},
"penalties": {
"homeTeam": null,
"awayTeam": null
}
},
"homeTeam": {
"id": 74,
"name": "West Bromwich Albion FC"
},
"awayTeam": {
"id": 340,
"name": "Southampton FC"
},
"referees": [
{
"id": 11430,
"name": "Simon Hooper",
"role": "MAIN_REFEREE",
"nationality": "England"
},
{
"id": 11570,
"name": "Harry Lennard",
"role": "ASSISTANT_N1",
"nationality": "England"
},
{
"id": 11505,
"name": "Derek Eaton",
"role": "ASSISTANT_N2",
"nationality": "England"
},
{
"id": 11585,
"name": "Craig Pawson",
"role": "FOURTH_OFFICIAL",
"nationality": "England"
},
{
"id": 11487,
"name": "Kevin Friend",
"role": "VIDEO_ASSISTANT_REFEREE",
"nationality": "England"
}
]
},
{
"id": 303253,
"competition": {
"id": 2002,
"name": "Bundesliga",
"area": {
"name": "Germany",
"code": "DEU",
"ensignUrl": "https://upload.wikimedia.org/wikipedia/commons/b/ba/Flag_of_Germany.svg"
}
},
"season": {
"id": 599,
"startDate": "2020-09-18",
"endDate": "2021-05-15",
"currentMatchday": 28,
"winner": null
},
"utcDate": "2021-04-12T18:30:00Z",
"status": "SCHEDULED",
"matchday": 28,
"stage": "REGULAR_SEASON",
"group": "Regular Season",
"lastUpdated": "2021-04-12T14:42:35Z",
"odds": {
"homeWin": 2.8,
"draw": 3.72,
"awayWin": 2.36
},
"score": {
"winner": null,
"duration": "REGULAR",
"fullTime": {
"homeTeam": null,
"awayTeam": null
},
"halfTime": {
"homeTeam": null,
"awayTeam": null
},
"extraTime": {
"homeTeam": null,
"awayTeam": null
},
"penalties": {
"homeTeam": null,
"awayTeam": null
}
},
"homeTeam": {
"id": 2,
"name": "TSG 1899 Hoffenheim"
},
"awayTeam": {
"id": 3,
"name": "Bayer 04 Leverkusen"
},
"referees": []
}]}
这是我尝试循环遍历对象的matches部分,我希望能够遍历每个匹配,同时仍然能够访问每个迭代中的子对象,并在我的cloud firestore数据库中设置数据
axios.request(options).then(function(response) {
//console.log(typeof response); // check the type of response returning already in JSON format
//console.log(response); // check the response object and based on key/values process it
const data = response.data; // if resp contains the data you will get it here.
//console.log(response.data);
/* for (let data1 in data.pagination) {
// from the sample response you shared in the question
console.log(data1) // prints the keys
console.log(data.pagination.data1) // prints the values*/
for (i = 0; i < data.count; i++) {
// from the sample response you shared in the question
// console.log(index);
// console.log(data.f[data1]) // prints the keys
// //console.log(data.pagination.data1) // prints the values
const soccerData = {
name: `${data.matches[i].competition.name}`,
id: `${data.matches[i].competition.id}`
};
return db.collection('matches').doc(`${data.matches[i].id}`)
.set(soccerData).then(() =>
console.log('data written to database'));
}
}).catch(function(error) {
console.error(error);
});
axios.request(选项)。然后(函数(响应){
//console.log(typeof response);//检查返回JSON格式的响应类型
//console.log(response);//检查响应对象并基于键/值对其进行处理
const data=response.data;//如果resp包含数据,您将在此处获得它。
//console.log(response.data);
/*for(让data1进入data.pagination){
//根据您在问题中分享的示例回答
console.log(data1)//打印密钥
console.log(data.pagination.data1)//打印值*/
对于(i=0;i
log(“数据写入数据库”);
}
}).catch(函数(错误){
控制台错误(error);
});
要循环匹配,请使用:
response.data.matches.forEach(match=>{
//对比赛做些什么
}
)