在提示中复制文本 – Node.js

我正在处理一个Node.js程序,它接受input并将其添加到列表中。 我将通过terminal使用这个程序。 我写了下面的函数。

问题领域:

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; } 

问题是,所有的input下一个重复,而我input它。

输出:

在这里输入图像说明

我所有的代码(如果你需要的话):

 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的noob所以如果你发现任何错误,请告诉我。

谢谢,基地

 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; } } }); } 

我基本上删除了你在调用case结束之后调用的getCommand() ,并且调用它,其中一个在switch case的case "list" ,另一个在函数toDo.add()

我想,当你像以前一样调用getCommand()时,都会提示在控制台上执行的contenttimeManage ,这也许就是为什么当你键入单个字母时得到双字母的原因。

这里是一个图像来演示你的代码发生了什么。 prompt.start()中的toDo.add()和getCommand prompt.start()中的toDo.add()之后的“getCommand”,我已经安慰了文本“Add”

在这里输入图像说明