【问题标题】:How to use foreach in foreach loop iin node js can anyone help me? [closed]如何在节点 js 的 foreach 循环中使用 foreach 谁能帮助我? [关闭]
【发布时间】:2019-07-14 23:44:15
【问题描述】:

我有两个表,表 1 有多个图像 ID,这些图像存储在表 2 中,我正在使用 for 循环,但由于使用 for 循环,它给了我无法发送标题错误。所以我该怎么办帮助 我正在使用 mongo db

【问题讨论】:

标签: javascript node.js for-loop


【解决方案1】:

使用“异步”NPM。首先安装 npm,

npm install async --save

然后在你的代码中使用如下,

const async = require('async');

async.forEach(Table1Ids, function (item, callback){ 
    console.log(item); // print the id or object

    table2.findOne({id: item.id}, function(err, doc){
        if(err){
            callback(err)
        }else if (doc){
            //Image fetch success. Use the doc here.
            callback();
        }else {
            callback();
        }
    });
}, function(err) {
    console.log('iterating done');
    //You can respond from here.
});

希望对你有帮助...

【讨论】:

  • 它有效,但只提供一个 id 图像数据并且还给我无法发送标题imgsafe.org/image/e5fed40224
  • 我在 cmets 部分看到了您的代码图像。您正尝试多次响应单个请求。这是不可能的。
  • 试试这个, 1. 找到图片ID,发送给客户端。 2. 从客户端,您可以循环图像 id 的数组并为它们中的每一个发出单独的请求。
  • 我可以在 php 中执行此操作吗?我们使用 sql 查询,其中 $varaiblae 和 $varaib;e 有多个用逗号分隔的 id。这个可以在 mongo db 节点中实现吗
  • 是的,可以做到。这实际上是一个 Node 和 Mongo DB 代码。
猜你喜欢
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多