1

I am trying to execute the following nodejs/mongoDB code:

var tickers = [];

MongoClient.connect(mongoUrl, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server."); //ok
  var cursor = db.collection('STI').find();
  cursor.each(function(err, doc){ 
      assert.equal(err, null);
      console.log(doc.symbol); //executes fine
      tickers.push(doc.symbol); 
  })
});

console.log(tickers);

The symbols are logging out fine to the console but after that the code throws an error 'TypeError: Cannot read property 'symbol' of null'.

The code seems to forget the doc.symbol by the time it gets to executing the 'tickers.push' part. How do I fix this?

Update:

I tried shifting the console.log(tickers) into the cursor.each callback and it prints out the array which each iteration, so the symbol pushing is happening. however i still get the same error

Update: full error message

/Users/kevin/Projects/yahooscrape/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^

TypeError: Cannot read property 'symbol' of null at /Users/kevin/Projects/yahooscrape/index.js:21:19 at handleCallback (/Users/kevin/Projects/yahooscrape/node_modules/mongodb/lib/utils.js:96:12) at /Users/kevin/Projects/yahooscrape/node_modules/mongodb/lib/cursor.js:736:16 at handleCallback (/Users/kevin/Projects/yahooscrape/node_modules/mongodb/lib/utils.js:96:12) at /Users/kevin/Projects/yahooscrape/node_modules/mongodb/lib/cursor.js:670:5 at handleCallback (/Users/kevin/Projects/yahooscrape/node_modules/mongodb-core/lib/cursor.js:154:5) at setCursorDeadAndNotified (/Users/kevin/Projects/yahooscrape/node_modules/mongodb-core/lib/cursor.js:463:3) at nextFunction (/Users/kevin/Projects/yahooscrape/node_modules/mongodb-core/lib/cursor.js:644:7) at Cursor.next [as _next] (/Users/kevin/Projects/yahooscrape/node_modules/mongodb-core/lib/cursor.js:685:3) at nextObject (/Users/kevin/Projects/yahooscrape/node_modules/mongodb/lib/cursor.js:655:8)

17
  • What is the result of that console.log inside of the each function? Also note that because the MongoClient.connect method is async, the very bottom console.log will always log an empty array. As the response from mongo probably hasn't even come back yet. You can move it into the connect block, right under the each function. Feb 16, 2016 at 19:34
  • But do elaborate on what the value is when you log it out. The value that SHOULD be pushed into the tickers array Feb 16, 2016 at 19:36
  • the console.log() in the each function logs out the doc.symbols
    – Kevin L.
    Feb 16, 2016 at 19:37
  • I guess my question is, what is doc.symbols? Feb 16, 2016 at 19:39
  • 'A17U.SI', 'BN4.SI', 'BS6.SI', 'C07.SI', 'C09.SI', 'C31.SI', 'C38U.SI', 'C52.SI', 'C6L.SI', 'CC3.SI', 'D05.SI', 'E5H.SI', 'F34.SI', 'G13.SI', 'H78.SI', 'MC0.SI', 'N21.SI', 'NS8U.SI', 'O39.SI', 'S51.SI', 'S58.SI', 'S59.SI', 'S63.SI', 'S68.SI', 'T39.SI', 'U11.SI', 'U14.SI', 'U96.SI', 'Y92.SI', 'Z74.SI'
    – Kevin L.
    Feb 16, 2016 at 19:40

1 Answer 1

0

Use cursor.forEach() instead of cursor.each(). Cursor.forEach() is an officially implemented mongodb method that does not throw the errors shown in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.