Why date sent from event.sender.send turn empty object?


#1

I’m trying send a new Date() from event.sender.send to client side
In my browser, has register a listener for event, where will console.log data received, but it’s print object { } when data is a Date type variable

How to can I fix this problem? Or how to can I send date from server side to client side, without stringify?


#2

Maybe it works when you send a timestamp, send that over IPC and then create a date from it:

timestamp = Date.now();

// ... ipc stuff

date = new Date(timestamp);

#3

Maybe this is work, but I need send array of json returned from MongoDB, to client side.

Maybe is not good need handle all register before to send.

Why can I send Date object with sender?


#4

You need to convert your dates to a format that is JSON serializable before sending them over IPC. Javascript’s built-in date type serializes as a timestamp, but doesn’t inflate back into a Date when run through JSON.parse (it just remains a timestamp string).

MongoDB’s Date type is probably a custom javascript object which doesn’t implement toJSON at all.

It’s also good to note that you can’t send Javascript Error objects through the IPC module either—they’ll come up as {} on the other side. You need to convert them to JSON yourself, or send just error.message, error.stack, etc.