英文:
getting weird mongodb object on client
问题
以下是翻译好的部分:
"well as i mentioned in the title when i'm sending message through socket to the server then save it in database mongodb with mongoose, then i returned the message and send it back in the socket,
now when i tried to print it on console in the server right before i send it to the client with socket,
i got the object i wanted to send, but when i checked the object i got in the client, then i got diffrent object seems related to mongo probably(pretty sure) and i'm not sure what should i do to fix it.this is what i get in the server right before i send it back to the client
this what i get in the client when i recieve new message"
const addMessage = async (newMessage) => {
try {
if (newMessage.type === 2) {
const audioBlob = new Buffer.from(newMessage.message).toString("base64");
newMessage.message = new Binary(audioBlob, Binary.SUBTYPE_BYTE_ARRAY);
}
const newMsg = new Message(newMessage);
await newMsg.save();
newMsg.message = Buffer.from(newMsg.message.buffer, "base64")
return newMsg;
} catch (error) {
console.log(error);
errorHandler(error);
}
};
i expected to get the same object i see in the server so in the client too
英文:
well as i mentioned in the title when i'm sending message through socket to the server then save it in database mongodb with mongoose, then i returned the the message and send it back in the socket,
now when i tried to print it on console in the server right before i send it to the client with socket,
i got the object i wanted to send, but when i checked the object i got in the client, then i got diffrent object seems related to mongo probably(pretty sure) and i'm not sure what should i do to fix it.this is what i get in the server right before i send it back to the client
this what i get in the client when i recieve new message
const addMessage = async (newMessage) => {
try {
if (newMessage.type === 2) {
const audioBlob = new Buffer.from(newMessage.message).toString("base64");
newMessage.message = new Binary(audioBlob, Binary.SUBTYPE_BYTE_ARRAY);
}
const newMsg = new Message(newMessage);
await newMsg.save();
newMsg.message = Buffer.from(newMsg.message.buffer, "base64")
return newMsg;
} catch (error) {
console.log(error);
errorHandler(error);
}
};
i expected to get the same object i see in the server so in the client too
答案1
得分: 0
你在提问时应始终提供代码片段。
上述问题的可能原因是,您正在打印console.log(result._docs)
并将完整的result
对象发送到数据库。也尝试将result._docs
发送到您的数据库。
附注:result
只是一个您分配数据的变量。在您的代码中,此变量可能会不同。
(如果这不起作用,请编辑您的问题并附上代码)
英文:
you should always, provide code snippets while asking a question.
Th probable reason for the above problem is, you are printing the console.log(result._docs)
and sending to the databse the complete result
object. Try sending result._docs
to your database as well.
P.S: result
is just a variable to which your assigning the data. This variable may be different in your code.
(If this does not work, edit your question and attach code)
答案2
得分: 0
I found the problem, though I'm not sure how to describe it, but the problem was here:
const newMsg = new Message(newMessage);
await newMsg.save();
newMsg.type === 2
? Buffer.from(newMsg.message.buffer, "base64")
: newMsg.message;
return newMsg;
So I took the newMessage and inserted newMsg.toJSON() to it:
if (newMessage.type === 2) {
const audioBlob = new Buffer.from(newMessage.message).toString("base64");
newMessage.message = new Binary(audioBlob, Binary.SUBTYPE_BYTE_ARRAY);
}
const newMsg = new Message(newMessage);
await newMsg.save();
newMessage = newMsg.toJSON();
if (newMessage.type === 2) {
newMessage.message = Buffer.from(newMessage.message.buffer, "base64");
}
return newMessage;
And now it's working!
英文:
well I found the problem, though i'm not sure how to describe it but the problem was here:
const newMsg = new Message(newMessage);
await newMsg.save();
newMsg.type === 2
? Buffer.from(newMsg.message.buffer, "base64")
: newMsg.message;
return newMsg;
so i took the newMessage and inserted newMsg.toJSON() to it;
if (newMessage.type === 2) {
const audioBlob = new Buffer.from(newMessage.message).toString("base64");
newMessage.message = new Binary(audioBlob, Binary.SUBTYPE_BYTE_ARRAY);
}
const newMsg = new Message(newMessage);
await newMsg.save();
newMessage = newMsg.toJSON();
if (newMessage.type === 2) {
newMessage.message = Buffer.from(newMessage.message.buffer, "base64");
}
return newMessage;
and now it's working!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论