2016-03-28 16 views
1

Girdi alan ve onu bir listeye ekleyen bir Node.js programı üzerinde çalışıyorum. Bu programı terminal üzerinden kullanacağım. Aşağıdaki işlevi yazdım.Sorgu içindeki metin çoğaltılıyor - Node.js

Sorun Alanları:

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

O zaman bu anahtar komutu exicutes denir.

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

Sorun, girdiğimde sonraki girdilerden sonraki tüm girdilerdir.

Çıktı:

enter image description here

Bütün Kodu (eğer ihtiyacınız varsa):

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: Ben node.js çaylak Eğer herhangi bir hata nokta eğer öyleyse söyle lütfen am .

sayesinde Taban

cevap

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

Ben anahtarı durumunda case "list" ve işlevi içinde birbirinin içinde, temelde getCommand() sen anahtarı durumunda bitiminden sonra çağırıyorlardı çıkarılıp aramış toDo.add()

Daha önce olduğu gibi getCommand() öğesini çağırdığınızda, her ikisi de content ve timeManage için konsolda çalıştırılır ve bu nedenle tek bir harf yazdığınızda neden çift harfi almanızın nedeni sorulur.

İşte kodunuzda ne olduğunu göstermek için bir görüntü. Metin "Add" teselli gelmiş toDo.add() içinde prompt.start() ve getCommand()

enter image description here

yılında prompt.start() sonra "getCommand" den sonra