我具有以下firebase云功能:
export const setFlightAlert = functions.database.ref('/trips/{pushId}/milestones').onCreate(async (snapshot) => {
let milestones: Milestone[] = [];
for (let milestone of snapshot.val()) {
let alert_id = 0;
if (+milestone.type.key === MilestoneTypes.departed_airport) {
const request = await setAlertId(milestone);
alert_id = request;
milestone = {
...milestone,
alert_id: alert_id,
ident: milestone.airline.name + milestone.flight_number
};
} else if (+milestone.type.key === MilestoneTypes.arrived_airport) {
milestone = {
...milestone,
alert_id: alert_id,
ident: milestone.airline.name + milestone.flight_number
};
}
milestones = [...milestones, milestone];
}
return snapshot.ref.set(milestones)
})
async function setAlertId(milestone: Milestone) {
let alert_id;
const data = new FormData();
data.append('alert_id', 0);
data.append('ident', `${milestone.airline.name}${milestone.flight_number}`);
data.append('origin', milestone.airport.icao);
data.append('date_start', new Date(milestone.flight_time).getTime() / 1000);
data.append('date_end', new Date(milestone.flight_time).getTime() / 1000);
data.append('chennels', '{16 e_filed e_departure e_arrival e_diverted e_cancelled}');
alert_id = await axios.post<{SetAlertResult: number}>(urls.setAlert, data, {
headers: {...headers, ...data.getHeaders()},
withCredentials: true
})
return alert_id.data.SetAlertResult;
}
当该函数被触发时,它应该从对setAlertId()
的异步等待调用中获取alert_ad的值。但是,当它运行时,我在云功能日志中收到一条错误消息,提示它未定义。我不确定为什么会这样。
Error: Reference.set failed: First argument contains undefined in property 'trips.-LZztKpsQxr_9p3p4-D3.milestones.0.alert_id'
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1442:15)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1488:13
at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/index.node.cjs.js:846:13)
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1471:14)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1488:13
at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/index.node.cjs.js:846:13)
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1471:14)
at validateFirebaseDataArg (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1430:5)
at Reference.set (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:14026:9)
at Object.<anonymous> (/user_code/lib/index.js:46:70)
我该如何测试或记录此信息,以便确保异步/等待实际上正在获取值?
答案 0 :(得分:1)
与任何JavaScript环境一样,您可以使用console.log()
来记录值。它们最终将在Firebase控制台中可见。
也可以考虑进行local testing以便于测试和调试。