arrays - MongoDB/NodeJS : Looping through query parameter

标签 arrays node.js mongodb foreach

我再次需要你的帮助,因为 querying through various Collections 之后(感谢@S.D.的帮助!),我想循环遍历查询的参数,这变得非常令人沮丧,因为我再一次不知道发生了什么......

我有这个代码:

var mongodb = require("mongodb");
let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"];

mongodb.connect("mongodb://localhost:27017/orderInfo", function(err, db) {
  if (err) {
    throw err;
  }

  db.listCollections().toArray((err, cols) => {
    if (err) {
      throw err;
    }

    cols.forEach(col => {
      pairs.forEach(p => {
        db
          .collection(col.name)
          .findOne({ pair: "BTC/EUR" }, {}, function(err, docs) {
            if (err) {
              throw err;
            }
            console.log(col.name + "[OK]: " + docs.pair);
          });
      });
    });
  });
});

这似乎工作得很好:

Gatecoinorder[OK]: BTC/EUR
Gatecoinorder[OK]: BTC/EUR
Gatecoinorder[OK]: BTC/EUR
Gatecoinorder[OK]: BTC/EUR
Coinmateorder[OK]: BTC/EUR
Coinmateorder[OK]: BTC/EUR
Coinmateorder[OK]: BTC/EUR
Krakenorder[OK]: BTC/EUR
Krakenorder[OK]: BTC/EUR
Coinmateorder[OK]: BTC/EUR
Krakenorder[OK]: BTC/EUR
Quoineorder[OK]: BTC/EUR
Cexorder[OK]: BTC/EUR
Livecoinorder[OK]: BTC/EUR
Gdaxorder[OK]: BTC/EUR
Virwoxorder[OK]: BTC/EUR
Bl3porder[OK]: BTC/EUR
Bitlishorder[OK]: BTC/EUR
Paymiumorder[OK]: BTC/EUR
Coinfloororder[OK]: BTC/EUR
Lakebtcorder[OK]: BTC/EUR
Anxproorder[OK]: BTC/EUR
Bitbayorder[OK]: BTC/EUR
Fybseorder[OK]: BTC/EUR
Itbitorder[OK]: BTC/EUR
_1btcxeorder[OK]: BTC/EUR
Fybsgorder[OK]: BTC/EUR
Dsxorder[OK]: BTC/EUR
Vaultoroorder[OK]: BTC/EUR
Krakenorder[OK]: BTC/EUR
Bitstamporder[OK]: BTC/EUR
Cexorder[OK]: BTC/EUR
Livecoinorder[OK]: BTC/EUR
Gdaxorder[OK]: BTC/EUR
Therockorder[OK]: BTC/EUR
Bl3porder[OK]: BTC/EUR
Bitlishorder[OK]: BTC/EUR
Paymiumorder[OK]: BTC/EUR
Wexorder[OK]: BTC/EUR
Lakebtcorder[OK]: BTC/EUR
Anxproorder[OK]: BTC/EUR
Bitbayorder[OK]: BTC/EUR
Exmoorder[OK]: BTC/EUR
Itbitorder[OK]: BTC/EUR
_1btcxeorder[OK]: BTC/EUR
Fybsgorder[OK]: BTC/EUR
Coinsecureorder[OK]: BTC/EUR
Vaultoroorder[OK]: BTC/EUR
Bitstamporder[OK]: BTC/EUR
Quoineorder[OK]: BTC/EUR
Livecoinorder[OK]: BTC/EUR
Gdaxorder[OK]: BTC/EUR
Therockorder[OK]: BTC/EUR
Virwoxorder[OK]: BTC/EUR
Bitlishorder[OK]: BTC/EUR
Paymiumorder[OK]: BTC/EUR
Wexorder[OK]: BTC/EUR
Coinfloororder[OK]: BTC/EUR
Anxproorder[OK]: BTC/EUR
Bitbayorder[OK]: BTC/EUR
Exmoorder[OK]: BTC/EUR
Fybseorder[OK]: BTC/EUR
_1btcxeorder[OK]: BTC/EUR
Fybsgorder[OK]: BTC/EUR
Coinsecureorder[OK]: BTC/EUR
Dsxorder[OK]: BTC/EUR
Bitstamporder[OK]: BTC/EUR
Quoineorder[OK]: BTC/EUR
Cexorder[OK]: BTC/EUR
Livecoinorder[OK]: BTC/EUR
Therockorder[OK]: BTC/EUR
Virwoxorder[OK]: BTC/EUR
Bl3porder[OK]: BTC/EUR
Bitlishorder[OK]: BTC/EUR
Wexorder[OK]: BTC/EUR
Coinfloororder[OK]: BTC/EUR
Lakebtcorder[OK]: BTC/EUR
Anxproorder[OK]: BTC/EUR
Exmoorder[OK]: BTC/EUR
Fybseorder[OK]: BTC/EUR
Itbitorder[OK]: BTC/EUR
_1btcxeorder[OK]: BTC/EUR
Coinsecureorder[OK]: BTC/EUR
Dsxorder[OK]: BTC/EUR
Vaultoroorder[OK]: BTC/EUR
Bitstamporder[OK]: BTC/EUR
Quoineorder[OK]: BTC/EUR
Cexorder[OK]: BTC/EUR
Gdaxorder[OK]: BTC/EUR
Therockorder[OK]: BTC/EUR
Virwoxorder[OK]: BTC/EUR
Bl3porder[OK]: BTC/EUR
Paymiumorder[OK]: BTC/EUR
Wexorder[OK]: BTC/EUR
Coinfloororder[OK]: BTC/EUR
Lakebtcorder[OK]: BTC/EUR
Bitbayorder[OK]: BTC/EUR
Exmoorder[OK]: BTC/EUR
Fybseorder[OK]: BTC/EUR
Itbitorder[OK]: BTC/EUR
Fybsgorder[OK]: BTC/EUR
Coinsecureorder[OK]: BTC/EUR
Dsxorder[OK]: BTC/EUR
Vaultoroorder[OK]: BTC/EUR

我可以看到查询有效地循环遍历数组的每个项目,然后返回 BTC/EUR 的查询。但是,当我将参数 pair: "BTC/EUR" 更改为 "p":

var mongodb = require("mongodb");
let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"];

mongodb.connect("mongodb://localhost:27017/orderInfo", function(err, db) {
  if (err) {
    throw err;
  }

  db.listCollections().toArray((err, cols) => {
    if (err) {
      throw err;
    }

    cols.forEach(col => {
      pairs.forEach(p => {
        db.collection(col.name).findOne({ pair: p }, {}, function(err, docs) {
          if (err) {
            throw err;
          }
          console.log(col.name + "[OK]: " + docs.pair);
        });
      });
    });
  });
});

我得到以下信息:

Gatecoinorder[OK]: ETH/EUR
Gatecoinorder[OK]: BTC/EUR
/Users/ardzii/Documents/NodeJS/Invest-Fund-Crypto/node_modules/mongodb/lib/utils.js:123
    process.nextTick(function() { throw err; });
                                  ^

TypeError: Cannot read property 'pair' of null

显然,该查询适用于 Gatecoinorder 的 ETH/EUR 和 BTC/EUR,但我猜想,由于 Gatecoinorder 没有 LTC/EUR 或 BCH/EUR 文档,循环就会停止。 我不知道为什么它会停止,我的意思是,即使 Gatecoinorder Collection 没有 LTC 和 BCH 文档,它也不应该停在那里,不是吗? 该错误表示属性“pair”为 null,这意味着没有集合? 我尝试在循环内添加 and if (col) 但没有帮助:

var mongodb = require("mongodb");
let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"];

mongodb.connect("mongodb://localhost:27017/orderInfo", function(err, db) {
  if (err) {
    throw err;
  }

  db.listCollections().toArray((err, cols) => {
    if (err) {
      throw err;
    }

    cols.forEach(col => {
      pairs.forEach(p => {
        if (col)
          db.collection(col.name).findOne({ pair: p }, {}, function(err, docs) {
            if (err) {
              throw err;
            }
            console.log(col.name + "[OK]: " + docs.pair);
          });
      });
    });
  });
});

如何避免此错误?

提前致谢!

最佳答案

这是因为您可能没有包含某些指定对的文档。 你必须处理这个问题

var mongodb = require("mongodb");
let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"];

mongodb.connect("mongodb://localhost:27017/orderInfo", function(err, db) {
  if (err) {
    throw err;
  }

  db.listCollections().toArray((err, cols) => {
    if (err) {
      throw err;
    }

    cols.forEach(col => {
      pairs.forEach(p => {
        if (col)
          db.collection(col.name).findOne({ pair: p }, {}, function(err, docs) {
            if (err) {
              throw err;
            }
            if(docs && docs.pair)
              console.log(col.name + "[OK]: " + docs.pair);
            else
              console.log(col.name + "[NOTOK]: No pair ");
          });
      });
    });
  });
});

关于arrays - MongoDB/NodeJS : Looping through query parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47590469/

相关文章:

node.js - 如何在 Mongoose 中按子文档字段对记录进行分组?

javascript - 管理数据库中用户之间的关系

javascript - 将数组转换为键/值数组

c++ - C++ 中的编译器如何解析数组声明?

c++ - 在堆上分配巨大的数组;程序不会崩溃并且编译没有错误

node.js - 使用 Nodejs Sharp 模块时出错。模块解析失败 : Unexpected character '' (1: 0)

node.js - 如何在 Angular 应用程序中提供 txt 文件

javascript - 尝试隐藏我的 API key 会破坏我的 URL。有人知道为什么吗?

node.js - 用于保存任何数据的 Mongodb 动态模式

java - 在JAVA中存储格式化名称的数组?