Search code examples
electronelectron-forge

The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object


I want to create a save file of pokemon data using json once someone clicks on the button "create pokemon data" after filling the text input and answering all the questions in the html but for some reason, I get this error

(node:30348) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, 
or DataView. Received an instance of Object
    at Object.writeFile (node:fs:2106:5)
    at E:\Pogramming folder\Pokehunt Streamers Tool\app\pokehunt\src\index.js:48:10

Here's my code this is the renderer.js

const fs = require('fs');
const {ipcRenderer} = require('electron');

var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
let hunt = 
    {
        "name": "",
        "imageurl": "",
        "method": "",
        "timer": "00:00:00",
        "counter" : 0,
        "shinycharm": false
    };

document.getElementById("startBtn").addEventListener("click",()=>{
    console.log("click!");
    const pokename = document.getElementById("pokemon").value;
    if(pokename!=null){
        hunt.name = pokename;
        hunt.imageurl = 'https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/${hunt.name}.gif';
        const selectMethod = document.getElementById("Method");
        hunt.method = selectMethod.value;
        const shinycharm = document.getElementById("shiny");
        switch(shinycharm){
            case "no":
                hunt.shinycharm = false;
                break;
            case "yes":
                hunt.shinycharm = true;
                break;
        }
        //for testing I did this log stuff
        console.log(JSON.stringify(hunt));
        const huntfinal = JSON.stringify(hunt);
        ipcRenderer.send("create-document-trigger",huntfinal);
    } else{
        hunt.name = "shinx";
    }
});

this is my main.js

const { app, BrowserWindow, ipcMain, nativeTheme, dialog } = require('electron');
const path = require('path');
const fs = require('fs');

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}
let mainWindow;
const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 955,
    height: 975,
    autoHideMenuBar: true,
    webPreferences:{
      enableRemoteModule: true,
      nodeIntegration: true,
      contextIsolation: false,
    }
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  //dark mode
  ipcMain.handle('dark-mode:toggle', () => {
    if (nativeTheme.shouldUseDarkColors) {
      nativeTheme.themeSource = 'light'
    } else {
      nativeTheme.themeSource = 'dark'
    }
    return nativeTheme.shouldUseDarkColors
  })

  ipcMain.handle('dark-mode:system', () => {
    nativeTheme.themeSource = 'system'
  })

  ipcMain.on("create-document-trigger",(hunt)=>{
    dialog.showSaveDialog(mainWindow,{
      filters: [{name:"json files",extensions:["json"]}]
    }).then(({filePath}) =>{
      console.log("filepath: "+filePath);
      fs.writeFile(filePath,hunt,(err)=>{
        if(err){
          console.log(err.message);
        }
      })
    })
  })
};


app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

the console.log give me {"name":"shinx","imageurl":"https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/${hunt.name}.gif","method":"pokeradar","timer":"00h:00m:00s","counter":0,"shinycharm":false} which is almost (the gif URL not putting the pokemon name but that's another problem I will solve later) exactly what I need inside the json file but the code fail to create this json file.


Solution

  • I was able to fix the issue.

    I replaced ipcMain.on("create-document-trigger",(hunt)=>{ to ipcMain.on("create-document-trigger",(event,data) =>{

    now the code looks like this and it works

      ipcMain.on("create-document-trigger",(event,data) =>{
        dialog.showSaveDialog(mainWindow,{
          filters: [{name:"json files",extensions:["json"]}]
        }).then(({filePath}) =>{
          console.log("filepath: "+filePath);
          fs.writeFile(filePath,data,(err)=>{
            if(err){
              console.log(err.message);
            }
          })
        })
      })