【问题标题】:MongoDB/NodeJS: Looping through query parameterMongoDB/NodeJS:循环查询参数
【发布时间】:2018-05-15 09:16:40
【问题描述】:

我再次需要您的帮助,因为在querying through various Collections(感谢@SD 的帮助!)之后,我想循环查询的参数,这变得非常令人沮丧,因为我再一次没有对正在发生的事情的想法......

我有这个代码:

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 文件,它也不应该停止,不是吗? 错误说属性“对”为空,这意味着没有集合? 我尝试在循环中添加和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);
          });
      });
    });
  });
});

如何避免此错误?

提前致谢!

【问题讨论】:

  • 由于console.log(col.name + "[OK]: " + docs.pair); 行,您遇到了问题。您正在尝试读取nullpair 属性,如您所说的LTC/EURBCH/EUR docs 为空

标签: arrays node.js mongodb foreach


【解决方案1】:

这是因为您可能没有包含某些指定对的文档。 你将不得不处理它

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 ");
          });
      });
    });
  });
});

【讨论】:

  • 通常,当您不能 100% 确定 b 存在于 a 上时,您应该学会警惕访问内部对象 ex a.b.c。当您从数据源(例如 API、数据库等)查询数据时,这很常见。
  • 确实!实际上,这个错误帮助我理解了 NodeJS 的 MongoDB 驱动程序实际上将 findOne({ xxx: 'xxxx"}) 翻译为:find the document where doc.pair = 'xxxx'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2021-06-08
  • 1970-01-01
  • 2020-08-20
  • 2012-05-09
  • 2016-10-16
相关资源
最近更新 更多