简体   繁体   English

Node.js提示跳过输入

[英]Node.js prompt skipping input

I'm currently taking a course on Coursera, and doing an exercise using node.js code to calculate a quadratic expression. 我目前正在Coursera上学习课程,并使用node.js代码进行练习来计算二次表达式。 All the code is given and this exercise is merely to get us to know node.js, but still I'm encountering a problem entering a prompt. 所有的代码都给出了,这个练习只是为了让我们知道node.js,但我仍然遇到输入提示的问题。
the code is here: 代码在这里:

var quad = require('./quadratic');

var prompt = require('prompt');

prompt.get(['a', 'b', 'c'], function (err, result) {
    if (err) { return onErr(err); }
    console.log('Command-line input received:');
    console.log('a: ' + result.a);
    console.log('b: ' + result.b);
    console.log('c: ' + result.c);

        quad(result.a,result.b,result.c, function(err,quadsolve) {
            if (err) {
                 console.log('Error: ', err);
                }
                else {
             console.log("Roots are "+quadsolve.root1() + "  " + quadsolve.root2());
                }
       });
});

As you see, I'm using the prompt module, but when I enter the input for a , the cmd is skipping the input for b and requesting me to enter `c, which in turn of couse, resulting in an error. 正如你看到的,我使用的prompt模块,但是当我进入输入a ,在cmd是跳跃的输入b ,并要求我进入`C,这反过来又淡然的,从而导致错误。

在此输入图像描述

How to fix this issue, and why does it happen? 如何解决这个问题,为什么会这样?

Welcome to developing on windows! 欢迎来到windows上开发! Windows uses a carriage return in addition to a \\n line ending which is probably why you see this bug. Windows使用除了回车\\n行结束为什么你看到这个bug这可能是。 You can force prompt to tokenize on a regular expression like this, which should hopefully fix your issue: 您可以强制提示对这样的正则表达式进行标记,这有望解决您的问题:

  var schema = {
    properties: {
      a: { pattern: /^[0-9]+$/, message: 'a', required: true },
      b: { pattern: /^[0-9]+$/, message: 'b', required: true },
      c: { pattern: /^[0-9]+$/, message: 'c', required: true }
    }
  };

  prompt.get(schema, function (err, result) {
      // .. rest of your code
  });

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

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