簡単にするために、私はnode.jsアプリケーションを短縮しました。ノード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に存在するかどうかを1行ずつ読み込みます。まあ私は実際にそれの約7コピーを持っており、それはfalseからtrueにemailExist変数を変更します。ただし、falseに設定されている場合は、その関数を呼び出します。以下は私のコンソール出力です: Console Output
これはなぜ起こっているのですか?
クロムをインストールします。 端末で: 'node --inspect --debug --debug-brk path/to/script.js' –
特にreadlineに関するjavascriptの非同期コールバックを読みましょう。あなたのメインロジックが完了した後、あなたのreadlineコールバックが発生しています。 – DrC