gpt4 book ai didi

javascript - Nodejs 处理错误

转载 作者:行者123 更新时间:2023-12-03 04:20:23 25 4
gpt4 key购买 nike

我是 NodeJS 的新手,我总是遇到同样的错误:

/var/www/html/helpdesk/node_modules/ent/encode.js:8 throw new TypeError('Expected a String'); ^

TypeError: Expected a String at Object.encode (/var/www/html/helpdesk/node_modules/ent/encode.js:8:15) at Socket. (/var/www/html/helpdesk/apps.js:32:22) at emitOne (events.js:96:13) at Socket.emit (events.js:191:7) at /var/www/html/helpdesk/node_modules/socket.io/lib/socket.js:503:12 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9) /var/www/html/helpdesk/node_modules/ent/encode.js:8 throw new TypeError('Expected a String'); ^

如何处理这个问题?

var express = require('express');
var app = express();
var server = app.listen(3000);
var io = require('socket.io')(server);
var ent = require('ent');
var fs = require('fs');


io.on('connection', function(socket){

socket.emit('message', { content: 'Vous êtes bien connecté ! ', importance: '1' });


socket.on('message', function (message) {
/*console.log('Un client me parle ! Il me dit : ' + message);
console.log(socket.pseudo + ' me parle ! Il me dit : ' + message);
io.sockets.emit('message', { content: 'Vous êtes bien connecté ! '+
socket.pseudo, importance: '1' });*/

message = ent.encode(message);
io.sockets.emit('message', {pseudo: socket.pseudo, message: message});
});

socket.on('pseudo', function(pseudo) {
pseudo = ent.encode(pseudo);
socket.pseudo = pseudo;
});

socket.on('nouveau_client', function(pseudo) {
if(pseudo.length >= 1 && typeof pseudo=="string"){
pseudo = ent.encode(pseudo);
socket.pseudo = pseudo;
io.sockets.emit('nouveau_client', pseudo);
}

});



});

最佳答案

包含有关您的代码和您想要执行的操作的更多信息肯定会对您更有帮助。本着提供帮助的精神,我可以做出一些假设。

看来您正在使用 socket.io。看起来您正在使用 emit 函数。也许类似于 socket.emit(thing)io.emit(thing)。我的猜测是 thing 不是字符串。您可以检查它或JSON.stringify()它来查看!

关于javascript - Nodejs 处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43972142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com