node.jsmongodbelectronipcrendereripcmain

Error sending from webFrameMain: Error: Failed to serialize arguments


enter image description here

I am using electron and mongodb. When I use ipcRenderer and ipcMain, and save a new data for my database model, I get this error. If I set the sandbox:true in main.js, this error will diseppear but t I will lose the connection between my node.js and electron, so I can't set the sandbox:true.

Here is my main.js code:

const mongoose = require('mongoose');

const {  BrowserWindow, ipcMain } = require('electron')

const Task = require("./models/Task")

function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 700,
        webPreferences:{
           
            sandbox: false,
            nodeIntegration:true,
            contextIsolation: false,
        }
    })
    

    win.loadFile("src/index.html")
}

ipcMain.on("new-task",async (e,args) =>{
    const newTask = new Task(args)
    const taskSaved = await newTask.save()
    console.log(taskSaved)
    e.reply("new-task-created",taskSaved)
})
module.exports = {createWindow}

And here is my app.js code which I get the reply from ipcMain:

const taskForm = document.querySelector("#taskForm")
const taskName = document.querySelector("#taskName")
const taskDescription = document.querySelector("#taskDescription")
const {ipcRenderer} = require("electron")
// const electron = require("electron")
// const ipc = electron.ipcRenderer

taskForm.addEventListener("submit",e=>{
    e.preventDefault();


    const task = {
        name: taskName.value,
        description: taskDescription.value
    }

    
  
    ipcRenderer.send("new-task",task)
    
    taskForm.reset() 
})

ipcRenderer.on("new-task-created",(e,args)=>{
    console.log(args)
})

At the moment the data appear in the DB but I get that error.


Solution

  • Solved. check docs

    I convert the data to JSON. This is how it rendered.