Search code examples
javascriptprototypeelectronmomentjs

Handling moment objects out of scope


I pass an array of moment objects in electron using ipc from main to the renderer, both include moment library, but when I try to call moment's functions on those objects in the renderer, it's like they're not moment objects anymore.

That's the object:

enter image description here

If I try to call format or other moment methods I get an error.

I'd rather not use the private fields in that object, so how can I make those objects be moment's objects again?

Edit:

This is how I pass the objects:

main.js: mainWindow.webContents.send('error-lines', errorLines);

Printing errorLines (part of it):

[ { id: '6',
    date: moment("2017-11-01T07:25:36.000"),
    start: false },
  { id: '5',
    date: moment("2017-11-01T08:01:40.000"),
    start: false },
  { id: '5', date: moment("2017-11-01T16:46:32.000"), start: true },
  { id: '6', date: moment("2017-11-01T17:11:11.000"), start: true },

renderer:

ipc.on('error-lines', function (event, data) {
    console.log(data);
}

Solution

  • electron's ipc between process does not allow non-serializable objects and only sends plain object over, so this is expected. You may send only plain object and reconstruct object on receiver process side to have full object you want.