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
”””?
-
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
1 Answer
Reset to default 6When 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
版权声明:本文标题:javascript - node.js process.stdout.write TypeError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745236321a2649059.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论