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)

Kevin L.
  • 1,371
  • 11
  • 24
  • 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. – Dustin Stiles Feb 16 '16 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 – Dustin Stiles Feb 16 '16 at 19:36
  • the console.log() in the each function logs out the doc.symbols – Kevin L. Feb 16 '16 at 19:37
  • I guess my question is, what is doc.symbols? – Dustin Stiles Feb 16 '16 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 '16 at 19:40
  • That is a single symbol or the entire array symbols array? – Dustin Stiles Feb 16 '16 at 19:41
  • and you DO get that output in the console? – Dustin Stiles Feb 16 '16 at 19:41
  • My intent is to figure out what a single 'symbol' element would look like. Because your code above is absolutely correct. I cannot spot any reason why the value of doc.symbol would change. – Dustin Stiles Feb 16 '16 at 19:42
  • i tried shifting the console.log(tickers) into the cursor.each callback and it prints out the array, so you are right the pushing is happening. however i still get the same error – Kevin L. Feb 16 '16 at 19:44
  • Without further clarification I'm not sure what else I can offer. – Dustin Stiles Feb 16 '16 at 19:46
  • each symbol is a text string of the type 'abc.ef' – Kevin L. Feb 16 '16 at 19:47
  • does doc.symbol === that list you provided earlier? Or is that the resulting array. Please be specific about the *individual* value you are working with – Dustin Stiles Feb 16 '16 at 19:48
  • Awesome, can you paste your error message in here? – Dustin Stiles Feb 16 '16 at 19:49
  • there are 30 docs and each doc has a symbol which is a text string like 'A17U.SI' – Kevin L. Feb 16 '16 at 19:49
  • Because you should only throw if doc is undefined. if only the symbol property was missing, you would just push undefined as each element and javascript would silently ignore that, not throw an error. Maybe the error can shed some light. – Dustin Stiles Feb 16 '16 at 19:51
  • From what you are explaining, it is as if console.log is erasing the entire doc object (which we know is not true). So I suspect foul play elsewhere. – Dustin Stiles Feb 16 '16 at 19:52
  • Possible dupe of http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – JohnnyHK Feb 16 '16 at 20:11

1 Answers1

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.

Kevin L.
  • 1,371
  • 11
  • 24