繁体   English   中英

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

[英]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() 时,都会提示输入contenttimeManage在控制台上执行的位置,这可能是你在键入单个字母时得到双字母的原因。

这是一张图片,用于演示您的代码发生了什么。 我已安慰之后的文本“添加” prompt.start()toDo.add()之后和“getCommand” prompt.start()getCommand()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM