0
votes

I'm new at NodeJS and i got always this same error :

/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'); ^

How to handle this?

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);
            }

        });



});
1
Pass a string? Put here what are you trying to doVladimir G.
You need to show YOUR code that triggers this exception.jfriend00
The error may come from this line message = ent.encode(message); ` var ent = require('ent'); var fs = require('fs'); ........... socket.on('message', function (message) { message = ent.encode(message); io.sockets.emit('message', {pseudo: socket.pseudo, message: message}); }); ..............obidano

1 Answers

1
votes

It would definitely be far more helpful for you to include more information about your code and what you are trying to do. In the spirit of helping though I can make some assumptions.

It looks like you are using socket.io. It also looks like you are using the emit function. Maybe something like socket.emit(thing) or io.emit(thing). My guess is that thing is not a string. You can check it or JSON.stringify() it to see!