Firebase +节点:更新两个引用会导致304错误 [英] Firebase + Node: Updating two refs results in 304 errors
问题描述
我拥有的是一个在调用时重置ref的函数.仅更新一个参考时有效.
What I have is a function that resets a ref when called. It works when updating only one ref.
这是该函数该部分的代码:
This is the code for that part of the function:
if (streakVal !== 0) {
//reset
const uid = item.child('uid').val();
ref.child(uid).update({ streak: 0 }).catch(err => {
res.status(500).send(err);
});
}
我还想更新数据库其他部分中的另一个引用.我将其包含在同一函数中,因为唯一的区别是引用位置.然后,该部分如下所示:
I also want to update another ref in a different part of the database. I've included it in the same function, as the only difference is the ref location. The part then looks like this:
if (streakVal !== 0) {
//reset
const uid = item.child('uid').val();
ref.child(uid).update({ streak: 0 }).then(() => {
boardRef.child(uid).update({ score: 0 }).catch(err => {
res.status(500).send(err);
});
}).catch(err => {
res.status(500).send(err);
});
}
第一个代码段有效.但是,第二个结果是错误Function execution took 800 ms, finished with status code: 304
,我想知道为什么会这样以及如何解决它.也许我不熟悉Node,所以我没有正确地构造它.我确定这是两个引用的正确路径.这是全部功能:
The first snippet works. However, the second results in the error Function execution took 800 ms, finished with status code: 304
I'm wondering why this is and how to fix it. Maybe I'm not structuring it correctly as I'm new to Node. I'm sure that's the correct path to both refs. Here is the full function:
export const resetStreak = functions.https.onRequest((req, res) => {
const ref = db.ref('users');
const boardRef = db.ref('streakLeaderboard');
ref.once('value').then(snap => {
snap.forEach(item => {
const streakVal = item.child('streak').val();
const lastQuestTimestamp = item.child('lastQuest').val();
const today = new Date();
const d = new Date(lastQuestTimestamp);
if (sameDay(today, d) === false) {
if (streakVal !== 0) {
//reset
const uid = item.child('uid').val();
ref.child(uid).update({ streak: 0 }).then(() => {
boardRef.child(uid).update({ score: 0 }).catch(err => {
res.status(500).send(err);
});
}).catch(err => {
res.status(500).send(err);
});
}
}
})
}).catch(err => {
res.status(500).send(err);
});
谢谢!
推荐答案
You should return your Promise
in your HTTPS functions (docs). Your code should become:
export const resetStreak = functions.https.onRequest((req, res) => {
const ref = db.ref('users');
const boardRef = db.ref('streakLeaderboard');
return ref.once('value').then(snap => {
const promises = []
snap.forEach(item => {
const streakVal = item.child('streak').val();
const lastQuestTimestamp = item.child('lastQuest').val();
const today = new Date();
const d = new Date(lastQuestTimestamp);
if (sameDay(today, d) === false) {
if (streakVal !== 0) {
//reset
const uid = item.child('uid').val();
promises.push(ref.child(uid).update({ streak: 0 }).then(() => {
return boardRef.child(uid).update({ score: 0 });
}));
}
}
})
return Promise.all(promises);
}).then(() => {
res.sendStatus(200);
}).catch(err => {
res.status(500).send(err);
});
这篇关于Firebase +节点:更新两个引用会导致304错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!