簡體   English   中英

調試節點JS邏輯

[英]Debugging Node JS Logic

為簡單起見,我縮短了node.js應用程序。

在我的服務器中,我復制了一段代碼以嘗試找出問題所在。 從邏輯上講,它應該工作。

// Subscribed to email service
app.get('/subscribe', function(req, res) {
    var emailExist = false;
    // Email to add
    var from = req.query.from;
    // Iterate through file and check to see if the email given exist or not.

    var readFile = fs.createReadStream("./Database/Subscription.txt");

    var readline = rl.createInterface({
        input: readFile,
        terminal: false,
    });

    readline.on('line', function(line) {
        if (line == from) {
            emailExist = true;
            console.log(line + " " + emailExist);
        }
    });

    console.log("hello " + emailExist);

    // If email dosn't exist
    if (emailExist === false) {
        console.log("I am false and need to be created");

        fs.appendFile("./Database/Subscription.txt", from + "\n", function(err) {
            if (err) {
                return console.log(err);
            }
            console.log(from + " was added to the email subscription.");
        });
    }
});

如上面的片段所示,它逐行讀取以確定用戶提交的電子郵件是否存在於Subscription.txt中。 好吧,我實際上有大約7個副本,它會將emailExist變量從false更改為true。 但是,當它設置為false時,它將調用具有它的函數。 以下是我的控制台輸出: 控制台輸出

為什么會這樣呢?

最簡單的解決方案是您需要將所有內容移到readline事件處理程序中:

readline.on('line', function(line) {
    if (line == from) {
        emailExist = true;
        console.log(line + " " + emailExist);
    }


    console.log("hello " + emailExist);

    // If email dosn't exist
    if (emailExist === false) {
        console.log("I am false and need to be created");

        fs.appendFile("./Database/Subscription.txt", from + "\n", function(err) {
            if (err) {
                return console.log(err);
            }
            console.log(from + " was added to the email subscription.");
        });
    }
});

原因是readline不等待終端輸入。 相反,您將事件處理程序(您的on('line')函數)傳遞給它,如果有傳入輸入,它將調用該事件處理程序。 注意:readline是誰將調用該函數。 您只是將其傳遞給readline,而不是調用它。 因此on里面的函數將在將來而不是現在被調用(這就是某些語言將這種類型的編程稱為“ Futures”的原因)。

通過將邏輯重構為函數,可以稍微提高可讀性(並減少回調地獄):

function processEmail (exist, callback) {
    console.log("hello " + exist);

    // If email dosn't exist
    if (exist === false) {
        console.log("I am false and need to be created");

        fs.appendFile("./Database/Subscription.txt", from + "\n", function(err) {
            if (err) {
                if (callback) callback(err);
            }
            else {
                console.log(from + " was added to the email subscription.");
                callback(null);
            }
        });
    }
}

readline.on('line', function(line) {
    if (line == from) {
        emailExist = true;
        console.log(line + " " + emailExist);
    }

    processEmail(emailExist);
});

還有其他一些方法可以使代碼更易於閱讀,例如promise和async / await,但是在研究promise或async / await之前,請確保您了解異步代碼的工作方式以及回調的含義,因為它們不會消除異步的性質。代碼,只是使語法看起來有所不同。

暫無
暫無

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

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