triscuit312 triscuit312 - 1 year ago 42
Node.js Question

How to set up and access class properties in Node.js?

I'm having trouble acessing the data in objects I've created. I'm very new to JS and node, and I think my issue is how I'm initializing the variables, but I don't know.

Here's my initializations:

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var async = require('async');
var currentBoatList = [];
var BoatObjectList = [];

I have a class to create a boat's current info (taken from a database):

function CurrentBoatInfo(boatName) {
var name,MMSI,callSign,currentDate,positionJSON,status,speed,course;
database.collection('Vessels').find({"BoatName":boatName},{"sort":{DateTime:-1}}).toArray(function(error1,vessel) {
name = vessel[0].BoatName;
MMSI = vessel[0].MMSI;
callSign = vessel[0].VesselCallSign;
console.log(name); \\logs the boats name, so the variable is there

I have my db function that pulls recent boats, puts their names in a list, and then in another list creates objects for each boatname in the list:

EDIT: I see I'm unnecessarily connecting to the mongoDB multiple times, working the code to fix that, and clear up 'db' variable name.

var createBoats = function() {
MongoClient.connect('mongodb://localhost:27017/tracks', function(err,database){
if (err) {return console.dir(err); }
else {console.log("connect to db");}
database.collection('Vessels').find({"MostRecentContact": { "$gte": (new Date((new Date()).getTime() - (365*24*60*60*1000)))}}).toArray(function(error,docs) { //within a year
docs.forEach(function(entry, index, array) {
currentBoatList.push(entry.BoatName); //create list of boats
BoatObjectList.push(new CurrentBoatInfo(entry.BoatName,database));

and finally my server code that simply creates a server, and is supposed to log some information from each of the objects created above, but for some reason doesn't (output below):

var server = function() {
http.createServer(function handler(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
console.log(BoatObjectList); //array of CurrentBoatInfo objects, prints [CurrentBoatInfo {}, CurrentBoatInfo {}, CurrentBoatInfo {}]
console.log(BoatObjectList[0].name); //prints undefined
BoatObjectList.forEach(function(entry) {
var count = 0;
for(var propertyName in entry.CurrentBoatInfo) { //nothing from here prints
}).listen(1337, '');

The output I see is something like:

connect to db
DOCK HOLIDAY //boat names as they're being instantiated
[ CurrentBoatInfo {}, CurrentBoatInfo {}, CurrentBoatInfo {} ] //list of boat objects
undefined //the name of the first boat in the object list
[ CurrentBoatInfo {}, CurrentBoatInfo {}, CurrentBoatInfo {} ]

Thinking through this, I now think that my issue is that the createServer code runs, but doesn't log, and then when I access, it logs name (which when instantiated was undefined)... but how do I make createServer wait for the objects to be instantiated?

Answer Source

The obvious problem I can spot is this piece of code:

docs.forEach(function(entry, index, array) {
  currentBoatList.push(entry.BoatName); //create list of boats
  if (currentBoatList.length === array.length) {
    async.eachOf(currentBoatList, function(entry,index,array) {     
      BoatObjectList[index] = new CurrentBoatInfo(currentBoatList[index]); //create object of the boat's info
    }, server()); //create the server after creating the objects

The problem here is that the async.eachOf is asynchronous function running inside the synchronous docs.forEach function. Also, the second parameter of the async.eachOf should have a callback which must be called for every item in the array, as in:

    async.eachOf(array, function(item, index, callback) {     
      doSomethingAsync(function(err, result) {
         if (err) return callback(err);

         // do something with result
    }, function(err) {
      // this is called when all items of the array are processed
      // or if any of them had error, err here will contain the error
      if(err) {
        console.log("something went wrong", err);
      } else {

As you can see running the server() as a callback doesn't look right because you should make sure there is no error passed to the final callback first and then proceed.

For your case I don't see why you are using async.eachOf instead of simple currentBoatList.forEach because you are not doing any asynchronous operations inside the loop, you are just populating the BoatObjectList.

UPD your problem is that you are not waiting for your asynchronous operations to finish before you are assuming your variables are ready. I hope the following will give you the idea of how you should achieve what you need (note that I've renamed some variables and functions just to make them more clear and easy to understand):

database.collection('Vessels').find(query).toArray(function(err, vessels) {
  if (err) {
    // process error

  async.eachOf(vessels, function(vessel, index, next) {

    getVesselInfoByName(vessel.BoatName, function(err, info) {
      if (err) {
        return next(err);

  }, function(err) {
    if (err) {
      // process error

    // all vessels are processed without errors, vesselsInfoList is ready to be used

function getVesselInfoByName(vesselName, callback) {
  // do everything you need to get vessel info
  // once you have received the vesselInfo call callback

  // if any error happens return callback(err);
  // otherwise return callback(null, vesselInfo);

Generally speaking I would advise you to find out more about how node.js asyncronius functions work and then have a closer look at the async library documentation.