admin管理员组

文章数量:1415673

I am working on a simple function to create console-based prompts in node.js without the use of a bunch of extra libraries:

“““

function prompt(text, callback) { // Text can be a question or statement.
    'use strict';
    var input, output;

    process.stdout.write(text + ' ');
    process.stdin.addListener('readable', function read() { // Stream type *must* be correct!
        input = process.stdin.read();
        if (input) { // Wait for actual input!
            output = input.toString().slice(0, -2); // Slicing removes the trailing newline, an artifact of the 'readable' stream type.
            process.stdout.write('You typed: ' + output);
            process.stdin.pause(); // Stops waiting for user input, else the listener keeps firing.
            callback(output);
        }
    });
}

prompt('Enter some text:', process.stdout.write);
// Enter some text: x
// You typed: x_stream_writable.js:200
//   var state = this.writableState;
//
// TypeError: Cannot read property '_writableState' of undefined
//     ...

”””

As per the question nodejs: shore alias for process.stdout.write , the “““this””” can be undefined when called from an alias. However, I am not using aliases, but direct calls of “““process.stdout.write”””. The first instance, inside the “““read()””” function, works fine but the second instance, as part of the callback, does not. Even weirder is that “““console.log””” works just fine if I substitute it in the second instance at the callback, despite it supposedly being a mere wrapper for the “““process.stdout.write””” function. How can I bind a “““this””” to the string “““output””” or, if that that isn’t feasible, what else can I do to solve the error “““TypeError: Cannot read property '_writableState' of undefined”””?

I am working on a simple function to create console-based prompts in node.js without the use of a bunch of extra libraries:

“““

function prompt(text, callback) { // Text can be a question or statement.
    'use strict';
    var input, output;

    process.stdout.write(text + ' ');
    process.stdin.addListener('readable', function read() { // Stream type *must* be correct!
        input = process.stdin.read();
        if (input) { // Wait for actual input!
            output = input.toString().slice(0, -2); // Slicing removes the trailing newline, an artifact of the 'readable' stream type.
            process.stdout.write('You typed: ' + output);
            process.stdin.pause(); // Stops waiting for user input, else the listener keeps firing.
            callback(output);
        }
    });
}

prompt('Enter some text:', process.stdout.write);
// Enter some text: x
// You typed: x_stream_writable.js:200
//   var state = this.writableState;
//
// TypeError: Cannot read property '_writableState' of undefined
//     ...

”””

As per the question nodejs: shore alias for process.stdout.write , the “““this””” can be undefined when called from an alias. However, I am not using aliases, but direct calls of “““process.stdout.write”””. The first instance, inside the “““read()””” function, works fine but the second instance, as part of the callback, does not. Even weirder is that “““console.log””” works just fine if I substitute it in the second instance at the callback, despite it supposedly being a mere wrapper for the “““process.stdout.write””” function. How can I bind a “““this””” to the string “““output””” or, if that that isn’t feasible, what else can I do to solve the error “““TypeError: Cannot read property '_writableState' of undefined”””?

Share Improve this question asked Jun 27, 2017 at 12:00 JBTJBT 1772 gold badges4 silver badges11 bronze badges 1
  • If the goal is to prompt the user for input, why not at least use the built-in readline module instead of trying to do it all manually? – mscdex Commented Jun 27, 2017 at 12:08
Add a ment  | 

1 Answer 1

Reset to default 6

When process.stdout.write() is called, it is expecting to be bound to the process.stdout object.

You can simply bind the function passed as the callback:

prompt('Enter some text:', process.stdout.write.bind(process.stdout));

In addition, your input slice(0,-2) does not work on Linux (as the newline is only one character '\n' instead of Windows '\r\n') - it's probably easier just to use input.toString().trim() or look up os.EOL for a more OS-agnostic approach.

本文标签: javascriptnodejs processstdoutwrite TypeErrorStack Overflow