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)