MongoDB / NodeJS:循环查询参数

我需要你的帮助,因为通过查询各种集合 (谢谢@你的帮助!),我想循环查询的参数,这是非常令人沮丧的,因为,再一次,我不知道发生了什么…

我有这个代码:

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 

显然这个查询适用于Gatecoin订户的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); }); }); }); }); }); 

我怎样才能避免这个错误?

提前致谢!

这是因为你可能没有一些指定对的文档。 你将不得不处理

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