1

I have a function that sends a query to ipcMain and then waits for the SQL query to be ran. When the response comes back I loop over the returned data and display that on screen for the user to see.

For some reason whenever the data is return and I log it from the render process it is sorted alphabetically. I haven't been able to determine where/why this sorting is occurring.

It would seem to me based on console.logs that sometime between ipcMain sending the request and the render process receiving the data it is getting sorted.

Any insight would be apprciated.

I've logged in all the blatant places to see if I can diagnose where the ordering is coming from.

// function that gets the results
// This console.log is alphabetically sorted here
  runQuery(config){
    window.ipc.send('run-query', config);
    return new Promise((resolve, reject) => {
      window.ipc.on('run-query-reply', (event, result) => {
        console.log(result)
        resolve(result);
      });
    });
  }
// calls handler - it is not sorted on this console.log
ipcMain.on('run-query', async (event, arg) => {
  var results = await handlers.runQuery(event, arg, scope);
  console.log(results)
  event.sender.send('run-query-reply', results)
})
// handler function - it is not sorted on this console.log
  async function runQuery (){
    try {
      var connection = await new scope.sql(arg.server.database, arg.server.username, arg.server.password, config);
      return await connection.query(query).spread((results) => {
        console.log(results)
        return results;
      })
    } catch (error) {
      return scope.errorHandler(error.message)
    }
  }

Sample Data:

// Data in ipcMain would like like:
{
 "email": "test", 
 "contact-id":"1",
 "first-name":"test", 
 "allow-email":test"
}
// Data from render window.ipc
{
 "allow-email":"test", 
 "contact-id":"1",
 "email": "test", 
 "first-name":"test"
}

1 Answers1

1

I believe this is most probably an artifact of your console logging in Chrome.

Most probably your ipcMain logs in a terminal, preserving your object keys order.

Whereas your renderer window.ipc logs in Chromium dev tools console, which renders your object keys sorted alphabetically.

See also Show original order of object properties in console.log

ghybs
  • 47,565
  • 6
  • 74
  • 99
  • Thanks and that is an interested note, however, I am pushing the data into arrays that get displayed on the page as well and they are also alphabetically order instead of the original order of data. I have taken a screenshot which shows a query and the columns being returned in a alphabetical order: [link]https://imgur.com/EtIxXVq – TJ Southern May 06 '19 at 19:34
  • Now that sounds like a totally different story. Please make sure you provide an [MCVE](https://stackoverflow.com/help/mcve) should you expect a more relevant answer, or even provide those details (how you loop / push into your arrays) in a new question. – ghybs May 06 '19 at 20:24
  • I was able to resolve this using some clues from your original answer. It looks like chrome is re-organizing the object regardless of it's use in console.log or just iteration. I stringified the object in the main and then sent it, then parsed it from the renderer and everything is in correct order. – TJ Southern May 06 '19 at 20:29
  • Explicitly stringifying is indeed a more robust solution. You may still have been guessing right about electron reordering your object keys on its own during ipc, as a few other people have reported similar cpncerns, although not with enough details to determine the exact root cause. – ghybs May 06 '19 at 20:38