简体   繁体   English

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

[英]Duplicating text within prompt - Node.js

I am working on a Node.js program that takes input and adds it to a list.我正在开发一个接受输入并将其添加到列表中的 Node.js 程序。 I am going to use this program through terminal.我将通过终端使用这个程序。 I have written the following function.我编写了以下函数。

Problem Areas:问题领域:

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

It is called when this switch command exicutes.当此开关命令执行时调用它。

switch (command){
                    case "list":
                        toDo.print();
                        break;
                    case "add":
                        toDo.add();
                        break;
                }

The issue is that all the input next duplicates while I input it.问题是,当我输入它时,所有接下来的输入都会重复。

Output:输出:

在此处输入图片说明

All my Code (if you need it):我所有的代码(如果你需要的话):

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: I am a Node.js noob so if you spot any mistakes please tell me. Ps:我是一个 Node.js 菜鸟,所以如果你发现任何错误,请告诉我。

Thanks, Base谢谢,基地

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

I basically removed the getCommand() you were calling after the end of the switch case and called it in, one inside the switch case where case "list" and the other inside the function toDo.add()我基本上删除了你在 switch case 结束后调用的getCommand()并调用它,一个在 switch case 中,其中case "list"另一个在函数toDo.add()

I guess when you called getCommand() like before, both prompts for content and timeManage where executed on the console and that maybe the reason why you get double letter when you type a single letter.我猜当你像以前一样调用 getCommand() 时,都会提示输入contenttimeManage在控制台上执行的位置,这可能是你在键入单个字母时得到双字母的原因。

Here is an image to demonstrate what happened with your code.这是一张图片,用于演示您的代码发生了什么。 I have consoled the text "Add" after prompt.start() in toDo.add() and "getCommand" after prompt.start() in getCommand()我已安慰之后的文本“添加” prompt.start()toDo.add()之后和“getCommand” prompt.start()getCommand()

在此处输入图片说明

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

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