0

I have setup electron for my angular 8 project and I can use electron now to build desktop app from my angular 8 project. Here is the structure of my project:

 + dist
 + e2e
 + electron => which includes main.ts & tsconfig.json & dist, and inside dist is my main.js
 + node_modules
 + src

Here is my main.ts file

    import { app, BrowserWindow, ipcMain } from 'electron'
    import * as path from 'path'
    import * as url from 'url'
    import * as fs from "fs";

    let win: BrowserWindow

    app.on('ready', createWindow)

    app.on('activate', () => {
        if (win === null) {
            createWindow()
        }
    });
    function createWindow() {
        win = new BrowserWindow({ fullscreen: true })
        win.loadURL(
            url.format({
                pathname: path.join(__dirname, `/panelfinal/index.html`),
                protocol: 'file:',
                slashes: true,
            })
        )

        win.webContents.openDevTools()

        win.on('closed', () => {
            win = null
        })
    }
    ipcMain.on('getFiles', (event, arg) => {
        console.log(event + "this is event");
        console.log(arg + " this is arg");
        const files = fs.readdirSync(__dirname);
        console.log(files + " Electron");
        win.webContents.send('getFilesResponse', files)
    })

I want to read files from file system with electron and I have created a electron service file and from there I try to make call to Ipc to access the file system. Here is my electron service:

    export class ElectronService {
      public ipc: IpcRenderer

      constructor(private snackBar: SnackbarService) {
        if ((<any>window).require) {
          try {
            this.ipc = (<any>window).require('electron').ipcRenderer
          } catch (error) {
            throw error
          }
        } else {
          console.warn('not for this platform')
        }
      }
      async getFiles(plaques: string[]) {
        new Promise<string[]>((resolve, reject) => {
          this.ipc.once("getFilesResponse", (event, arg) => {
            resolve(arg);
          });
          this.ipc.send("getFiles", plaques);
        });

      }
    }

when I call the getFiles() method in my electron I get this error:

Cannot read property 'once' of undefined

Any ideas why?

Dale K
  • 25,246
  • 15
  • 42
  • 71
geekymano
  • 1,420
  • 5
  • 22
  • 53
  • 1
    Because it is inside a new Promise scope - look at this: https://stackoverflow.com/questions/42606632/angular2-cannot-access-this-inside-a-promise – Morten Frederiksen Oct 21 '19 at 13:23
  • @MortenFrederiksen I have changed the method like this: async getFiles(plaques: string[]) { let self = this; new Promise((resolve, reject) => { self.ipc.once("getFilesResponse", (event, arg) => { resolve(arg); }); self.ipc.send("getFiles", plaques); }); } but still get the error – geekymano Oct 21 '19 at 13:36
  • 1
    I just happened upon your post while researching an issue of my own. Have you seen https://dev.to/michaeljota/integrating-an-angular-cli-application-with-electron---the-ipc-4m18 ? I have not completed his tutorial yet, but it seems to be exactly the service your are creating. – Steve B Dec 23 '19 at 13:55

0 Answers0