英文:
NodeJS race condition when retrieving Firebase realtime database node data
问题
以下是您要翻译的内容:
出现以下Node.js竞争条件问题(我认为是这样)。
我正在尝试获取池ID <当前#> 是 <类型>
以下是我的代码,下面是输出:
for (; currPool <= lastPool;) {
console.log("Pool ID: " + currPool);
poolsRef.child(currPool).child("PoolType").once('value', function(snapshot) {
if (snapshot.exists()) {
const poolType = snapshot.val();
console.log("Pool ID:", currPool, "is a:", poolType, " Style PoolType");
}
});
currPool++;
}
输出如下:
Pool ID: 11018 is a: Survivor Style PoolType
Pool ID: 11019 is a: Pickem Style PoolType
Pool ID: 11020 is a: Pickem Style PoolType
Pool ID: 11021 is a: Pickem Style PoolType
Pool ID: 11022 is a: Pickem Style PoolType
Pool ID: 11023 is a: Survivor Style PoolType
如何让它显示:
Pool ID: 11018 is a: Survivor Style PoolType
Pool ID: 11019 is a: Pickem Style PoolType
Pool ID: 11020 is a: Pickem Style PoolType
Pool ID: 11021 is a: Pickem Style PoolType
Pool ID: 11022 is a: Pickem Style PoolType
Pool ID: 11023 is a: Survivor Style PoolType
英文:
Having an issue with the following nodejs race condition (i believe it called).
I am trying to get the Pool ID <current #> is a <type>
Below is my code and below that is the output:
for(;currPool<=lastPool;) {
console.log("Current Pool ID: " + currPool);
poolsRef.child(currPool).child("PoolType").once('value', function(snapshot) {
if (snapshot.exists()) {
const poolType = snapshot.val();
//
console.log("Pool ID:", currPool, "is a:", poolType , " Style PoolType");
}
});
currPool++;
}
The output is as follows:
Current Pool ID: 11018
Current Pool ID: 11019
Current Pool ID: 11020
Current Pool ID: 11021
Current Pool ID: 11022
Current Pool ID: 11023
Pool ID: 11024 is a: Survivor Style PoolType
Pool ID: 11024 is a: Pickem Style PoolType
Pool ID: 11024 is a: Pickem Style PoolType
Pool ID: 11024 is a: Pickem Style PoolType
Pool ID: 11024 is a: Pickem Style PoolType
Pool ID: 11024 is a: Survivor Style PoolType
How do i get it to display :
Pool ID: 11018 is a: Survivor Style PoolType
Pool ID: 11019 is a: Pickem Style PoolType
Pool ID: 11020 is a: Pickem Style PoolType
Pool ID: 11021 is a: Pickem Style PoolType
Pool ID: 11022 is a: Pickem Style PoolType
Pool ID: 11023 is a: Survivor Style PoolType
答案1
得分: 1
你在循环内部使用了回调函数。这是一个有很多问题的做法,像这里提到的那样。使用for
循环来执行任务并不理想。如果想使用for
循环,在for
语句中加入currPool++
。或者使用while
循环:
while (currPool <= lastPool) {
// 代码的其余部分
currPool++
}
根据文档,.on
接受一个回调函数,而.once
可以接受回调函数,但也返回一个Promise,这将有助于简化你的代码并解决异步问题:
;(async () => {
while (currPool <= lastPool) {
console.log("当前池ID:", currPool)
const snapshot = await poolsRef.child(currPool)
.child("PoolType")
.once("value")
if (snapshot.exists()) {
const poolType = snapshot.val()
console.log("池ID:", currPool, "是:", poolType, "风格的PoolType")
}
currPool++
}
})
英文:
You've got a callback inside a loop. There are lots of questions around about that issue, like this one, but getting your code to work with a for loop isn't ideal.
You're using a for loop to do the job a while loop. If you want to use a for loop, currPool++
in the for statement. Or use a while loop instead:
while (currPool <= lastPool) {
// rest of the code
currPool++
}
And according to the docs, .on
takes a callback, while .once
can take a callback, but also returns a Promise, which would help flatten your code and fix the async issue:
;(async () => {
while (currPool <= lastPool) {
console.log("Current Pool ID: ", currPool)
const snapshot = await poolsRef.child(currPool)
.child("PoolType")
.once("value")
if (snapshot.exists()) {
const poolType = snapshot.val()
console.log("Pool ID:", currPool, "is a:", poolType , " Style PoolType")
}
currPool++
}
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论