簡體   English   中英

如何在Node.js中設置和訪問類屬性?

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

我無法訪問我創建的對象中的數據。 我是JS和節點的新手,我認為我的問題是我如何初始化變量,但我不知道。

這是我的初始化:

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

我有一個班級來創建船的當前信息(取自數據庫):

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
        });
    });
}

我有我的數據庫功能,它可以提取最近的船只,將它們的名字放在一個列表中,然后在另一個列表中為列表中的每個船名創建對象:

編輯:我看到我多次不必要地連接到mongoDB,使用代碼來修復它,並清除'db'變量名。

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));
                });
                server();
            });
        });
};

最后我的服務器代碼只是創建一個服務器,並且應該記錄上面創建的每個對象的一些信息,但由於某種原因沒有(在下面輸出):

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
                console.log(JSON.stringify(propertyName));
                count++;
                console.log(count);
            }
        });
        res.end();
    }).listen(1337, '127.0.0.1');
};

我看到的輸出是這樣的:

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

考慮到這一點,我現在認為我的問題是createServer代碼運行,但不記錄,然后當我訪問127.0.0.1:1337時,它記錄名稱(實例化時未定義)...但是怎么做我讓createServer等待實例化對象?

我能發現的一個明顯問題是這段代碼:

docs.forEach(function(entry, index, array) {
  currentBoatList.push(entry.BoatName); //create list of boats
  if (currentBoatList.length === array.length) {
    console.log(currentBoatList);
    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
  }
});

這里的問題是async.eachOf是在同步docs.forEach函數內運行的異步函數。 此外, async.eachOf的第二個參數應該有一個必須為數組中的每個項調用的回調,如:

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

         // do something with result
         callback();
      });
    }, 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 {
        console.log("success");
      }
  });

正如您所看到的那樣,運行server()作為回調看起來不正確,因為您應該確保沒有錯誤首先傳遞給最終回調然后繼續。

對於你的情況,我不明白為什么你使用async.eachOf而不是簡單的currentBoatList.forEach,因為你沒有在循環中做任何異步操作,你只是填充BoatObjectList。

UPD您的問題是您在假設變量准備好之前不等待異步操作完成。 我希望以下內容能讓您了解如何實現所需(注意我已經重命名了一些變量和函數,以使它們更清晰易懂):

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

  async.eachOf(vessels, function(vessel, index, next) {
    currentBoatList.push(vessel.BoatName);

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

      vesselsInfoList.push(info);
      next();
    });
  }, function(err) {
    if (err) {
      // process error
      return;
    }

    // all vessels are processed without errors, vesselsInfoList is ready to be used
    server();
  });
});

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);
}

一般來說,我建議您了解有關node.js asyncronius函數如何工作的更多信息,然后仔細查看異步庫文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM