情况:
我需要下载 childNode,然后将其 set() 到另一个节点中。
问题是我只想在 childNode 的 Score 属性达到 100 时才执行此操作。
我应该在何时何地检查帖子的分数是否为 100 或更高,以及如何将它们复制到新索引一次?
我的想法:
加载帖子后,检查其分数。如果>= 100,请检查数据库是否属于这种情况。然后将该节点推送到新索引。
PROBLEM:
由于多次加载时得分 >= 100,如何防止每次加载帖子时都上传节点?我只需要它发生一次!
解决方案代码:
if (funPost.score >= global.hotNumber && funPost.hot == false) {
var hotPostRef = firebase.database().ref("hot/section/"+key);
var hotPost = {
title: funPost.title,
image: funPost.image,
id: funPost.id,
key: funPost.key
}
hotPostRef.set(hotPost);
funPostRef.update({"hot": true});
}
else if (funPost.score <= (global.hotNumber - 25) && funPost.hot == true) {
var hotPostRef = firebase.database().ref("hot/section/"+key);
hotPostRef.remove();
funPostRef.update({"hot": false});
}
解决方案:我最终使用了布尔标志。
我最终用一个布尔标志来做到这一点:
if (funPost.score >= global.hotNumber && funPost.hot == false) {
var hotPostRef = firebase.database().ref("hot/section/"+key);
var hotPost = {
title: funPost.title,
image: funPost.image,
id: funPost.id,
key: funPost.key
}
hotPostRef.set(hotPost);
funPostRef.update({"hot": true});
}
else if (funPost.score <= (global.hotNumber - 25) && funPost.hot == true) {
var hotPostRef = firebase.database().ref("hot/section/"+key);
hotPostRef.remove();
funPostRef.update({"hot": false});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)