作为一个基于日期将数据插入Firebase实时数据库的大型程序的一部分,我使用了以下函数,该函数在给定日期的情况下更新当天的数据。
/** Updates database for a single day
* @param {string} day - A date string in the format 'YYYY-MM-DD'
* @param {ApiManager} apiManager - An instance of ApiManager to fetch data
*/
async updateSingle(day, apiManager) {
idList.forEach(async id => {
let reference = ref(this.db, `${day}/${id}`);
let data = await apiManager.getDataApi(day, id, apiManager.season);
console.log(data); // undefined
set(reference, data);
});
}
每当这个函数运行时,它会给出下面的错误,我无法理解。我在几个月前写了这个程序,它运行得很好。当我几天前回到它,继续我的工作,这是我遇到的。
file:///c:/Users/xyz/node_modules/@firebase/database/dist/node-esm/index.node.esm.js:10636
throw new Error(errorPrefix + 'contains undefined ' + validationPathToErrorString(path));
^
Error: set failed: value argument contains undefined in property '2023-02-23.848'
at validateFirebaseData (file:///c:/Users/xyz/node_modules/@firebase/database/dist/node-esm/index.node.esm.js:10636:15)
at validateFirebaseDataArg (file:///c:/Users/xyz/node_modules/@firebase/database/dist/node-esm/index.node.esm.js:10628:5)
at set (file:///c:/Users/xyz/node_modules/@firebase/database/dist/node-esm/index.node.esm.js:12794:5)
at file:///c:/Users/xyz/src/firebase_manager.js:30:13
at processTicksAndRejections (node:internal/process/task_queues:96:5)
运行getDataApi()
会返回一个JSON响应,这是一个足球比赛JSON对象列表,如下所示。
[
{
fixture: {
id: 971801,
referee: 'Istvan Kovacs, Romania',
timezone: 'UTC',
date: '2023-02-21T20:00:00+00:00',
timestamp: 1677009600,
periods: [Object],
venue: [Object],
status: [Object]
},
league: {
id: 2,
name: 'UEFA Champions League',
country: 'World',
logo: 'https://media-3.api-sports.io/football/leagues/2.png',
flag: null,
season: 2022,
round: 'Round of 16'
},
teams: { home: [Object], away: [Object] },
goals: { home: 2, away: 5 },
score: {
halftime: [Object],
fulltime: [Object],
extratime: [Object],
penalty: [Object]
}
},
{
fixture: {
id: 971803,
referee: 'Artur Soares Dias, Portugal',
timezone: 'UTC',
date: '2023-02-21T20:00:00+00:00',
timestamp: 1677009600,
periods: [Object],
venue: [Object],
status: [Object]
},
league: {
id: 2,
name: 'UEFA Champions League',
country: 'World',
logo: 'https://media-3.api-sports.io/football/leagues/2.png',
flag: null,
season: 2022,
round: 'Round of 16'
},
teams: { home: [Object], away: [Object] },
goals: { home: 0, away: 2 },
score: {
halftime: [Object],
fulltime: [Object],
extratime: [Object],
penalty: [Object]
}
}
]
1条答案
按热度按时间qvk1mo1f1#
因此JSON标准不允许
undefined
作为值,这就是firebase在验证JSON时抛出错误的原因。您 * 确实 * 使用console.log记录了对象,但它没有递归显示所有嵌套对象。
要查找坏属性,请执行以下操作:
console.log(JSON.stringify(data))
在这里,您将看到以
undefined
为值的'2023-02-23.848'
属性在哪里,然后您可以为该键分配一些值,以使其工作。