javascript Node.js 类型错误:无效的非字符串/缓冲区块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17188533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js TypeError: Invalid non-string/buffer chunk
提问by HoloLady
ANSWER: I've finally got it, turns out I was also looking in the wrong place for the logger file. it places it in the main folder instead of the folder the javascript was in. The code that worked for me was:
ANSWER: 我终于明白了,原来我也在错误的地方寻找记录器文件。它将它放在主文件夹而不是 javascript 所在的文件夹中。对我有用的代码是:
var fs = require('fs');
var log = fs.createWriteStream('nodelogger.txt', {flags: 'a', encoding: 'utf-8',mode: 0666});
server.listen(8888);
app.use("/", express.static(__dirname + '/files'));
io.sockets.on('connection', function (socket) {
socket.on('msg', function (data) {
io.sockets.emit('new', data);
log.on('error', function (err) {
console.log(err);
});
console.log(data);
log.on('error', function(e) { console.error(e); });
var newdata = JSON.stringify(data);
log.write(newdata += "\r\n")
});
});
I'm totally new to Node.js so I could really use some help. I'm trying to log messages from the chat I'm making So far I have this:
我对 Node.js 完全陌生,所以我真的可以使用一些帮助。我正在尝试记录我正在进行的聊天中的消息到目前为止我有这个:
var fs = require('fs');
var log = fs.createWriteStream('nodelogger.txt', {'flags': 'a'});
server.listen(8888);
app.use("/", express.static(__dirname + '/files'));
io.sockets.on('connection', function (socket) {
socket.on('msg', function (data) {
io.sockets.emit('new', data);
log.write(data);
log.on('error', function (err) {
console.log(err);
});
});
});
But I keep getting this error:
但我不断收到此错误:
events.js:72
throw er; // Unhandled 'error' event
^
TypeError: Invalid non-string/buffer chunk
at validChunk (_stream_writable.js:150:14)
at WriteStream.Writable.write (_stream_writable.js:179:12)
at Socket.<anonymous> (/Users/test/Desktop/Chat/app.js:16:9)
at Socket.EventEmitter.emit [as $emit] (events.js:95:17)
at SocketNamespace.handlePacket (/Users/test/Desktop/Chat/node_modules/socket.io/lib/namespace.js:335:22)
at Manager.onClientMessage (/Users/test/Desktop/Chat/node_modules/socket.io/lib/manager.js:488:38)
at WebSocket.Transport.onMessage (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transport.js:387:20)
at Parser.<anonymous> (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transports/websocket/hybi-16.js:39:10)
at Parser.EventEmitter.emit (events.js:95:17)
at finish (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transports/websocket/hybi-16.js:288:16)
And I have no idea what it means, can't find it anywhere either. Does anybody have an idea what I'm doing wrong? Thanks in advance!
而且我不知道它是什么意思,也找不到任何地方。有人知道我做错了什么吗?提前致谢!
回答by Mayur Patil
Here you can convert datatype to string and print it. It will work as expected.
在这里您可以将数据类型转换为字符串并打印出来。它会按预期工作。