簡體   English   中英

JSON.stringify在新創建的對象(節點,javascript)上失敗

[英]JSON.stringify fails on a newly created object (node, javascript)

好的,這必須是一個非常簡單的問題,但是我才剛剛開始學習節點。 我也是javascript的新手,因此,請在下面指出錯誤的方向時不要大意。

特別是我有兩個文件:

  • 一個具有在不同端口創建一些從屬服務器的類
  • 另一個是生成從站的“主”文件

當我嘗試打印剛剛初始化的內容時,出現兩個奇怪的錯誤:

  • 連接屬性已棄用。 使用getConnections()方法,然后
  • 當我嘗試在新對象中應用JSON.stringify時崩潰(將循環結構轉換為JSON)

文件“ slave.js”中奴隸的代碼:

var http = require ("http");

function Slave () {
}

Slave.prototype.ID           = undefined;
Slave.prototype.coordinator  = false;
Slave.prototype.httpServer   = undefined;
Slave.prototype.thePort      = undefined;

Slave.prototype.isCoordinator = function () { return this.coordinator; }

/*****************************************************************/

function handle_incoming_request (req, res) {
    console.log("INCOMING REQUEST: " + req.method + " " + req.url);
    res.writeHead (200, { "Content-Type" : "application/json" });
    res.end( JSON.stringify({ "error" : null }) + "\n" );
}

exports.createSlave = function (id, coordinatorK, port) {
    var temp         = new Slave ();
    temp.ID          = id;
    temp.coordinator = coordinatorK;
    temp.thePort     = port;
    temp.httpServer  = http.createServer(handle_incoming_request);
    temp.httpServer.listen (temp.thePort);
    console.log ("Slave (" + (temp.isCoordinator() ? "coordinator" : "regular") + ") with ID " + temp.ID + " is listening to port " + temp.thePort);
    console.log ("--------------------------------------------");

    return temp;
}

現在,主文件。

var http   = require ("http");
var url    = require ("url");
var a      = require ("./slave.js");

var i, temp;
var myArray = new Array ();

for (i = 0; i < 4; i++) {
    var newID = i + 1;
    var newPort = 8000 + i + 1;
    var coordinatorIndicator = false;

    if ((i % 4) == 0) {
        coordinatorIndicator = true; // Say, this is going to be a coordinator
    }

    temp = a.createSlave (newID, coordinatorIndicator, newPort);
    console.log ("New slave is  : " + temp);
    console.log ("Stringified is: " + JSON.stringify(temp));

    myArray.push(temp);
}

您正在嘗試對http.createServer(...)的結果進行字符串化。 那不是您想要做的,因此在創建該屬性時,請使用Object.defineProperty()對其進行定義,使其不可枚舉。

exports.createSlave = function (id, coordinatorK, port) {
    var temp         = new Slave ();
    temp.ID          = id;
    temp.coordinator = coordinatorK;
    temp.thePort     = port;

    Object.defineProperty(temp, "httpServer", {
        value: http.createServer(handle_incoming_request),
        enumerable: false, // this is actually the default, so you could remove it
        configurable: true,
        writeable: true
    });
    temp.httpServer.listen (temp.thePort);
    return temp;
}

這樣JSON.stringify將無法達到該屬性,無法固化其對對象的枚舉。

問題是temp對象的屬性httpServer具有循環引用。 您可以使用上一個答案中提到的Ecmascript5屬性定義將其設置為不可枚舉,也可以使用JSON.stringify的replacer函數對其進行自定義,以httpServer屬性進行字符串化。

   console.log ("Stringified is: " + JSON.stringify(temp, function(key, value){
         if(key === 'httpServer') return undefined;
        return value;
    }));

暫無
暫無

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

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