[英]Duplicating text within prompt - Node.js
我正在開發一個接受輸入並將其添加到列表中的 Node.js 程序。 我將通過終端使用這個程序。 我編寫了以下函數。
問題領域:
add: function () {
console.log("What do you want to add to the ToDo List?");
// Starts the prompt
prompt.start();
// Gets input called content
prompt.get(['content'], function(err, result) {
content = result.content
// Pushed content to the list
toDo.list.push(content);
});
當此開關命令執行時調用它。
switch (command){
case "list":
toDo.print();
break;
case "add":
toDo.add();
break;
}
問題是,當我輸入它時,所有接下來的輸入都會重復。
輸出:
我所有的代碼(如果你需要的話):
var prompt = require('prompt');
// Empty variables that we will use for prompt input
var content = "";
var command = "";
// Exits the program when this variable changes state
var done = false;
// Object that holds all functions and data for the ToDo portion of this program
var toDo = {
// List that everything all ToDos will be stored within
list: ["Example", "Example 2"],
// Print function prints the list
print: function () {
console.log(toDo.list);
},
// The add function should add a value to the list
add: function () {
console.log("What do you want to add to the ToDo List?");
// Starts the prompt
prompt.start();
// Gets input called content
prompt.get(['content'], function(err, result) {
content = result.content
// Pushed content to the list
toDo.list.push(content);
});
}
}
// Main loop
function getCommand() {
// Starts the prompt
prompt.start();
// Ask for name until user inputs "done"
prompt.get(['timeManage'], function(err, result) {
command = result.timeManage;
// Checks if it is equal to exit; if so it exits the program
if (command === 'exit') {
console.log('Thanks for using timeManage.');
} else {
// Checks the remaining commands; if it finds one it executes
switch (command){
case "list":
toDo.print();
break;
case "add":
toDo.add();
break;
}
// Loops the prompt unless the word exit is run
getCommand();
}
});
}
getCommand();
Ps:我是一個 Node.js 菜鳥,所以如果你發現任何錯誤,請告訴我。
謝謝,基地
var toDo = {
// List that everything all ToDos will be stored within
list: ["Example", "Example 2"],
// Print function prints the list
print: function () {
console.log(toDo.list);
},
// The add function should add a value to the list
add: function () {
console.log("What do you want to add to the ToDo List?");
// Starts the prompt
prompt.start();
// Gets input called content
prompt.get(['content'], function(err, result) {
content = result.content
// Pushed content to the list
toDo.list.push(content);
getCommand();
});
}
}
function getCommand() {
// Starts the prompt
prompt.start();
// Ask for name until user inputs "done"
prompt.get(['timeManage'], function(err, result) {
command = result.timeManage;
// Checks if it is equal to exit; if so it exits the program
if (command === 'exit') {
console.log('Thanks for using timeManage.');
} else {
// Checks the remaining commands; if it finds one it executes
switch (command){
case "list":
toDo.print();
getCommand();
break;
case "add":
toDo.add();
break;
}
}
});
}
我基本上刪除了你在 switch case 結束后調用的getCommand()
並調用它,一個在 switch case 中,其中case "list"
另一個在函數toDo.add()
我猜當你像以前一樣調用 getCommand() 時,都會提示輸入content
和timeManage
在控制台上執行的位置,這可能是你在鍵入單個字母時得到雙字母的原因。
這是一張圖片,用於演示您的代碼發生了什么。 我已安慰之后的文本“添加” prompt.start()
在toDo.add()
之后和“getCommand” prompt.start()
在getCommand()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.