Return Rows through IPC in Electronjs

Asked

Viewed 19 times

0

Save my friends. I’m studying a little bit about Electron, and I came across the following problem in interprocessual communication.

I have an index.html (rendered process) that emits an event pro main.js (main process). This event is a search query in the database. I managed to get everything right, from the input in index (rendered), to the query in main. However, now I want to return the result of this query to index again.

Below, respectively, the function that executes the query (argQuery is the index.html input received through IPC, so far everything worked right).

MAIN.JS

const { app, BrowserWindow, nativeImage, ipcMain } = require("electron");

function selectSearch(argQuery) {
  connection.query(
    "SELECT * FROM alunos WHERE nome LIKE concat('%', ?, '%')", argQuery,
    function(err, rows, fields) {
        return rows;
    }
  );
}

And the eventListener that answers the rendered process, and also, performs the above function.

MAIN.JS

ipcMain.on("query-alunos", (event, args) => {

  // args = recebido do input do index.html
  const recievedInput = args;

  //executa a function acima
  selectSearch(recievedInput, (rows, fields) => {
    // como minha ideia é fazer uma filtering list, o tamanho de rows é dinâmico
    // pesquisar E => retorna 200 alunos, pesquisar Julio, retorna 50, etc
    rows.forEach(element => {
      console.log("Sending to Rendered ->" + element.nome);
      //em tese deveria mandar pro rendered os nomes retornados.
      event.sender.send("fetch-alunos", element.nome)
    });
  });
});

and then, at index (rendered), I would have another Liener to listen to this "fetch-students" and do something with the names returned.

This was logic that came into my head at first, but is returning Undefined. In case anyone can explain to me, I am grateful!

In case someone needs the rendered code

INDEX.HTML

         const { ipcRenderer } = require('electron')

         function sendToMain(processName, toSend){
               ipcRenderer.send(processName, toSend);
                //...code irrelevante pra pergunta que cortei
            }

          // final listener que pega o resultado da query e faz algo
          ipcRenderer.on("fetch-alunos", (event, args) => {
               alert("Querys Chagam ao Rendered caso tudo esteja OK");
            }
          );
            
         //envia o value do input para cada letra digitada no input
         function searchAluno(){
            if (event.keyCode >= 65 && event.keyCode <= 90) {
                  const nomeAluno = document.getElementById("inputTodo").value;
                  sendToMain("query-alunos", nomeAluno);
            } else { 
               console.log("Invalid Event")
            }

         }
  • Why don’t you return event.sender.send a single time, sending the list instead of sending N times, sending one name at a time? And the alert in the Nderer is being shown, only that args is undefined, that’s it?

  • I tried already, main.js (simplified) ipcMain.on("query-alunos", (event, args) => {connection.query("SELECT * FROM alunos WHERE nome LIKE concat('%', ?, '%')", args, (rows, fields) => {event.sender.send("fetch-alunos", rows)});}); In case Alert fires, but passing the Rows as args, in rendered it returns null. index.html (rendered) ipcRenderer.on("fetch-alunos", (event, args) => {console.log("Aqui eram pra voltar as querys finais mas retorna null "+args);});

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.