1

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!

Martin Carre
  • 1,157
  • 4
  • 20
  • 42
  • you are getting problem because of this `console.log(col.name + "[OK]: " + docs.pair);` line. you are trying to read the `pair` property of `null` as you said in case of `LTC/EUR` & `BCH/EUR` `docs` is null – Surendra Agarwal Dec 01 '17 at 09:48

1 Answers1

1

It because you might not have docs with some of the specified pairs. you will have to handle that

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 ");
          });
      });
    });
  });
});
Stamos
  • 3,938
  • 1
  • 22
  • 48
  • 1
    Usually you should learn to be wary of accessing inner object ex `a.b.c` when you are not 100% sure that `b` exists on `a`. That is very common when you are querying data from a data source (ex API, Database, etc). – Stamos Dec 01 '17 at 10:02
  • Indeed! Actually this error helped me understand that somehow the MongoDB driver for NodeJS actually translates `findOne({ xxx: 'xxxx"})` by: `find the document where doc.pair = 'xxxx'` – Martin Carre Dec 01 '17 at 10:11