每种方法如何中断
How break for each method
我正在处理预订系统。我使用 firebase 实时数据库。所以我想删除过去的数据(历史上,在今天之前)。我决定对 this.I 使用 firebase 函数尝试使用 if else (我不知道是否有更简单的方法)但我不能使用 break 和 return 命令。删除了今天之前的日子怎么才能停止呢
数据库结构:https://prnt.sc/AyubX0SQG3oo
exports.removePrev =
functions.pubsub.schedule("every 5 minutes").onRun((context) => {
const day = new Date();
const targetDateFormat = new Date(day).toLocaleDateString("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit"});
const resultDay = targetDateFormat.replace(/[/]/g, "-");
admin.database().ref("reservations/").once("value").then((snap)=>{
snap.forEach((days)=>{
if (days.key != resultDay) {
admin.database().ref("reservations/"+days.key).remove();
} else if (days.key==resultDay) {
//Want to break;
}
});
});
return null;
});
forEach
不支持 break
,检查这个答案
Short circuit Array.forEach like calling break
或者您可以使用 for of
,MDN 文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
我正在处理预订系统。我使用 firebase 实时数据库。所以我想删除过去的数据(历史上,在今天之前)。我决定对 this.I 使用 firebase 函数尝试使用 if else (我不知道是否有更简单的方法)但我不能使用 break 和 return 命令。删除了今天之前的日子怎么才能停止呢
数据库结构:https://prnt.sc/AyubX0SQG3oo
exports.removePrev =
functions.pubsub.schedule("every 5 minutes").onRun((context) => {
const day = new Date();
const targetDateFormat = new Date(day).toLocaleDateString("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit"});
const resultDay = targetDateFormat.replace(/[/]/g, "-");
admin.database().ref("reservations/").once("value").then((snap)=>{
snap.forEach((days)=>{
if (days.key != resultDay) {
admin.database().ref("reservations/"+days.key).remove();
} else if (days.key==resultDay) {
//Want to break;
}
});
});
return null;
});
forEach
不支持 break
,检查这个答案
Short circuit Array.forEach like calling break
或者您可以使用 for of
,MDN 文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of