I'd need your help again because after querying through various Collections (Thanks @S.D. for your help!), I would like to loop through the parameter of the query and this is getting very frustrating because, once more, I have no idea of what's happening...
I have this code:
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);
});
});
});
});
});
That seems to be working just fine:
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
I can see that the query is effectively looping through each item of the array an then returning the query for BTC/EUR. However when I change the parameter pair: "BTC/EUR"
to "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);
});
});
});
});
});
I get the following:
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
Apparently the query works for ETH/EUR and BTC/EUR for Gatecoinorder but I guess that since Gatecoinorder has no LTC/EUR or BCH/EUR documents well the loop just simply stops.
I have no idea why it stops, I mean, even though the Gatecoinorder Collection has no LTC and BCH documents it shouldn't stop there, should it?
The error says property 'pair' of null which would mean there's no Collection?
I've tried to add and if (col)
within the loop and it didn't help:
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);
});
});
});
});
});
How can I avoid this error?
Thanks in advance!