NodeJS在检索Firebase实时数据库节点数据时的竞态条件

huangapple go评论75阅读模式
英文:

NodeJS race condition when retrieving Firebase realtime database node data

问题

以下是您要翻译的内容:

出现以下Node.js竞争条件问题(我认为是这样)。

我正在尝试获取池ID <当前#> 是 <类型>

以下是我的代码,下面是输出:

for (; currPool &lt;= 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&lt;=lastPool;) {

    console.log(&quot;Current Pool ID: &quot; + currPool);

    poolsRef.child(currPool).child(&quot;PoolType&quot;).once(&#39;value&#39;, function(snapshot) {
        
        if (snapshot.exists()) {
        
            const poolType = snapshot.val();
            //
            console.log(&quot;Pool ID:&quot;, currPool, &quot;is a:&quot;, poolType , &quot; Style PoolType&quot;);

         }

    });

    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 &lt;= 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 () =&gt; {
    while (currPool &lt;= lastPool) {
        console.log(&quot;Current Pool ID: &quot;, currPool)
        const snapshot = await poolsRef.child(currPool)
            .child(&quot;PoolType&quot;)
            .once(&quot;value&quot;)
        if (snapshot.exists()) {
            const poolType = snapshot.val()
            console.log(&quot;Pool ID:&quot;, currPool, &quot;is a:&quot;, poolType , &quot; Style PoolType&quot;)
        }
        currPool++
    }
})

huangapple
  • 本文由 发表于 2023年5月7日 09:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定