0

I have a socket.io based chat server that saves messages to MongoDB. When a user sends something like ಠ_ಠ it is emitted through the socket and displayed correctly, but when I take that same message and save it to MongoDB through Mongoose I end up with ಠ_ಠsaved in the database.

I thought Mongoose/MongoDB/socket.io all spoke UTF-8 by default...why is the data being converted to ASCII when I save it to the DB?

socket.on('newMessage', function(data){

var data = JSON.parse(data);

var chatMessage = new ChatMessage();
chatMessage.user = data.userId;
chatMessage.room = data.roomId;
chatMessage.message = data.message.toString("utf8");
chatMessage.save(function(err, message){
  // Emits ಠ_ಠ
  socket.broadcast.to(data.roomId).emit('deliverMessage', JSON.stringify(message));
});

I'm even emitting what was saved to MongoDB (message not data.message), but MongoDB has ಠ_ಠstored....SOOOOOO confused :s

I understand encoding and why it matters. My question is which part of the socket.io, mongoDB, mongoose pipeline is switching the encoding and why when everything seems to be speaking uft-8?

SomethingOn
  • 9,813
  • 17
  • 68
  • 107
  • When I copy/paste your ಠ_ಠ into a string and save it in the mongo shell (2.6.5), I get ಠ_ಠ saved to the database successfully. Can you confirm this happens with your version of mongo? In that case, I think the problem is with mongoose or the driver. Can you try a test insertion of "ಠ_ಠ" with just the node driver, not using mongoose? – wdberkeley Oct 13 '14 at 14:28
  • Yes, when I save ಠ_ಠ to MongoDB directly is saves just fine and it does the same through my REST API which also uses Mongoose. The problem only occurs when I save the ಠ_ಠ string from my socket.io project which also uses Mongoose...I'll double check my Mongoose versions to make sure they're the same. Thanks for the help...it's got me thinking ;) – SomethingOn Oct 14 '14 at 15:06
  • Yes, probably an encoding problem somewhere in the pipes, so you'll have to send your goofy face through them one by one and see which one changes it. – wdberkeley Oct 15 '14 at 16:49
  • Possible duplicate of [What is character encoding and why should I bother with it](https://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it) – Raedwald Nov 26 '18 at 12:22
  • I know what encoding is and why it matters.The question is which part of the socket.io, mongoDB, mongoose pipeline is switching the encoding and how would I got about determining that. – SomethingOn Nov 26 '18 at 18:30

0 Answers0