胡說叔叔
2023-03-03 16:17:34
我有一個(gè)錯(cuò)誤...如果某些東西已經(jīng)存在,我想檢查 firebase 的數(shù)據(jù)庫,否則轉(zhuǎn)到錯(cuò)誤捕獲鏈。但我找不到錯(cuò)誤。const ref = admin.database().ref(`/users/${uid}/programs/${program}/`) ref.once('value', snap => { if (snap.val() !== null) { const registered = Object.values(snap.val()); const kids = Object.keys(registered[0].registered); kids.forEach(kid => { if (children.includes(kid)) { throw new Error('already registered') } }) } }).then(() => { ... set to db不管它發(fā)生了什么,然后去趕上。
1 回答

慕尼黑的夜晚無繁華
TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
不要在異步回調(diào)中拋出錯(cuò)誤,就像您傳遞給once. 僅將它們放入承諾回調(diào)中:
const ref = admin.database().ref(`/users/${uid}/programs/${program}/`);
ref.once('value').then(snap => {
// ^^^^^
if (snap.val() !== null) {
const registered = Object.values(snap.val());
const kids = Object.keys(registered[0].registered);
kids.forEach(kid => {
if (children.includes(kid)) {
throw new Error('already registered')
}
})
}
// ... set to db
}).catch(err => {
// ...
});
添加回答
舉報(bào)
0/150
提交
取消