gpt4 book ai didi

javascript - 我不明白为什么 displayAllTodoItems() 在这段代码中被执行两次

转载 作者:行者123 更新时间:2023-12-03 00:32:35 25 4
gpt4 key购买 nike

给出的代码是一个用 NodeJS 制作的控制台 Todo 应用程序,它可以存储新的 Todo 项目、显示它们并且能够删除任何一项。我面临的问题是,当我向应用程序添加一个新的 Todo 项目,然后在不停止程序的情况下,我尝试显示使用 displayAllTodoItems() 存储的所有 Todo 项目,结果显示两次。但是,如果我在停止应用程序一次后执行 displayAllTodoItems() ,则不会发生这种情况。谁能告诉我为什么会这样?

const fs = require('fs');
const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let outputData;
console.log(`Welcome to Todo app!
-----------------------------------
Press 1 to add a new Todo item.
Press 2 to display all Todo items.
Press 3 to delete a Todo item.
Press 4 to exit.`);
takeInput();

function takeInput(){
rl.on("line", (answer) => {
checkSwitch(answer);
});
}

function checkSwitch(answer) {
if(answer == "1"){
rl.question("Give the name and description of the Todo item (separated by a comma):", (ans) => {
let [name, description] = ans.split(",");
addNewTodoItem(name, description);
});
}
else if(answer == "2"){
displayAllTodoItems();
}
else if(answer == "3"){
rl.question("Give the number of the Todo item to be deleted:", (ans) => {
let number = ans;
deleteATodoItem(number);
});
}
else if(answer == "4"){
console.log("Program exited!")
process.exit();
}
else {
console.log("Invalid selection!")
}
takeInput();

}

const addNewTodoItem = (name, description) => {
fs.readFile("todofile.txt", "UTF-8", (err) => {
if(err){
fs.writeFile("todofile.txt", `${name}:${description}\n`, (err) => {
if(err){
console.log("The data couldn't be saved.");
}
else{
console.log("The Todo item has been saved.");
}
});
}
else {
fs.appendFile("todofile.txt", `${name}:${description}\n`, (err) => {
if(err){
console.log("The data couldn't be saved.");
}
else{
console.log("The Todo item has been saved.");
}
});
}
});
}

const displayAllTodoItems = () => {
let text = fs.readFileSync("todofile.txt").toString('utf-8').split("\n");
text = text.filter(text=>text!="");
text.forEach((element, i)=> {
console.log(`${i+1}. ${element}`);
})
}
const deleteATodoItem = () => console.log("Delete");

我也发布了这个问题的答案。

最佳答案

看来 checkSwitch() 中的 takeInput() 函数调用是问题的原因。问题已经解决。一旦在程序开始执行 takeInput() 函数,rl.on("line", callback) 就会监听新行条目。当checkSwitch()函数中再次调用takeInput()时,rl.on("line",callback)被执行了两次,造成问题。这是应用程序的完整且更正的版本:

const fs = require('fs');
const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let outputData;
console.log(`Welcome to Todo app!
-----------------------------------
Press 1 to add a new Todo item.
Press 2 to display all Todo items.
Press 3 to delete a Todo item.
Press 4 to exit.`);

takeInput();

function takeInput() {
rl.on("line", (answer) => {
checkSwitch(answer);
});
}

function checkSwitch(answer) {
if (answer == "1") {
rl.question("Give the name and description of the Todo item (separated by a comma):", (ans) => {
let [name, description] = ans.split(",");
addNewTodoItem(name, description);
});
}
else if (answer == "2") {
displayAllTodoItems();
}
else if (answer == "3") {
rl.question("Give the number of the Todo item to be deleted:", (ans) => {
deleteATodoItem(ans);

});
}
else if (answer == "4") {
console.log("Program exited!")
process.exit();
}
else {
console.log("Invalid selection!");
}
}

const addNewTodoItem = (name, description) => {
fs.readFile("todofile.txt", "UTF-8", (err) => {
if (err) {
fs.writeFileSync("todofile.txt", `${name}:${description}\n`);
console.log("The Todo item has been saved.");
}
else {
fs.appendFile("todofile.txt", `${name}:${description}\n`, (err) => {
if (err) {
console.log("The data couldn't be saved.");
}
else {
console.log("The Todo item has been saved.");
}
});
}
});
}

const displayAllTodoItems = () => {
let text = fs.readFileSync("todofile.txt").toString('utf-8').split("\n");
text = text.filter(text => text != "");
text.forEach((element, i) => {
console.log(`${i + 1}. ${element}`);
});
}

const deleteATodoItem = (number) => {
let text = fs.readFileSync("todofile.txt").toString('utf-8').split("\n");
text = text.filter(text => text != "");
text.splice(number - 1, 1);
fs.writeFileSync("todofile.txt", text.join("\n"));
console.log("Item deleted.");
}

关于javascript - 我不明白为什么 displayAllTodoItems() 在这段代码中被执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53794062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com