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

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

NodeJS race condition when retrieving Firebase realtime database node data

问题

以下是您要翻译的内容:

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

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

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

  1. for (; currPool &lt;= lastPool;) {
  2. console.log("Pool ID: " + currPool);
  3. poolsRef.child(currPool).child("PoolType").once('value', function(snapshot) {
  4. if (snapshot.exists()) {
  5. const poolType = snapshot.val();
  6. console.log("Pool ID:", currPool, "is a:", poolType, " Style PoolType");
  7. }
  8. });
  9. currPool++;
  10. }

输出如下:

  1. Pool ID: 11018 is a: Survivor Style PoolType
  2. Pool ID: 11019 is a: Pickem Style PoolType
  3. Pool ID: 11020 is a: Pickem Style PoolType
  4. Pool ID: 11021 is a: Pickem Style PoolType
  5. Pool ID: 11022 is a: Pickem Style PoolType
  6. Pool ID: 11023 is a: Survivor Style PoolType

如何让它显示:

  1. Pool ID: 11018 is a: Survivor Style PoolType
  2. Pool ID: 11019 is a: Pickem Style PoolType
  3. Pool ID: 11020 is a: Pickem Style PoolType
  4. Pool ID: 11021 is a: Pickem Style PoolType
  5. Pool ID: 11022 is a: Pickem Style PoolType
  6. 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:

  1. for(;currPool&lt;=lastPool;) {
  2. console.log(&quot;Current Pool ID: &quot; + currPool);
  3. poolsRef.child(currPool).child(&quot;PoolType&quot;).once(&#39;value&#39;, function(snapshot) {
  4. if (snapshot.exists()) {
  5. const poolType = snapshot.val();
  6. //
  7. console.log(&quot;Pool ID:&quot;, currPool, &quot;is a:&quot;, poolType , &quot; Style PoolType&quot;);
  8. }
  9. });
  10. currPool++;
  11. }

The output is as follows:

  1. Current Pool ID: 11018
  2. Current Pool ID: 11019
  3. Current Pool ID: 11020
  4. Current Pool ID: 11021
  5. Current Pool ID: 11022
  6. Current Pool ID: 11023
  7. Pool ID: 11024 is a: Survivor Style PoolType
  8. Pool ID: 11024 is a: Pickem Style PoolType
  9. Pool ID: 11024 is a: Pickem Style PoolType
  10. Pool ID: 11024 is a: Pickem Style PoolType
  11. Pool ID: 11024 is a: Pickem Style PoolType
  12. Pool ID: 11024 is a: Survivor Style PoolType

How do i get it to display :

  1. Pool ID: 11018 is a: Survivor Style PoolType
  2. Pool ID: 11019 is a: Pickem Style PoolType
  3. Pool ID: 11020 is a: Pickem Style PoolType
  4. Pool ID: 11021 is a: Pickem Style PoolType
  5. Pool ID: 11022 is a: Pickem Style PoolType
  6. Pool ID: 11023 is a: Survivor Style PoolType

答案1

得分: 1

你在循环内部使用了回调函数。这是一个有很多问题的做法,像这里提到的那样。使用for循环来执行任务并不理想。如果想使用for循环,在for语句中加入currPool++。或者使用while循环:

  1. while (currPool <= lastPool) {
  2. // 代码的其余部分
  3. currPool++
  4. }

根据文档,.on接受一个回调函数,而.once可以接受回调函数,但也返回一个Promise,这将有助于简化你的代码并解决异步问题:

  1. ;(async () => {
  2. while (currPool <= lastPool) {
  3. console.log("当前池ID:", currPool)
  4. const snapshot = await poolsRef.child(currPool)
  5. .child("PoolType")
  6. .once("value")
  7. if (snapshot.exists()) {
  8. const poolType = snapshot.val()
  9. console.log("池ID:", currPool, "是:", poolType, "风格的PoolType")
  10. }
  11. currPool++
  12. }
  13. })
英文:

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:

  1. while (currPool &lt;= lastPool) {
  2. // rest of the code
  3. currPool++
  4. }

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:

  1. ;(async () =&gt; {
  2. while (currPool &lt;= lastPool) {
  3. console.log(&quot;Current Pool ID: &quot;, currPool)
  4. const snapshot = await poolsRef.child(currPool)
  5. .child(&quot;PoolType&quot;)
  6. .once(&quot;value&quot;)
  7. if (snapshot.exists()) {
  8. const poolType = snapshot.val()
  9. console.log(&quot;Pool ID:&quot;, currPool, &quot;is a:&quot;, poolType , &quot; Style PoolType&quot;)
  10. }
  11. currPool++
  12. }
  13. })

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:

确定