2

I have a function which am trying to get it to return a knex result from the the database using return but it does not seem to work? I am working with knex library not ajax.

Code

//function

function runKnex(){
        //Run Queries and send Content
        var result = knex.select().table('User')
         return result.then(function(rows){
            return rows;
        })
}

//calling function

mainWindow.webContents.on('did-finish-load',()=>{
        const knexres = runKnex();
        console.log(knexres);
});

Results

  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined
} 

Why is it not the array? How do i get it to return an array ?

NB when you console.log(rows) you get:

[
  { UserId: 1, FirstName: 'Tarik', LastName: 'Guney', Age: 30 },
  { UserId: 2, FirstName: 'Sumeyye', LastName: 'Guney', Age: 29 }
]
xaander1
  • 1,064
  • 2
  • 12
  • 40
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Seblor Sep 20 '19 at 12:29
  • 1
    thats talking about ajax and asynchronous calls man?? – xaander1 Sep 20 '19 at 12:39
  • The issue is the same, you are trying to return the return value of an asynchronous call. Right now, you are returning the promise. The point of duplicates are to point people to the correct solution, because some answers may applies to multiple issues, like yours. Having one's question flagged as duplicate is just a way to point to the answer, it's not a bad thing. – Seblor Sep 20 '19 at 12:40
  • a simpler explanation would have been nice anyway you did your thing. – xaander1 Sep 20 '19 at 12:55

1 Answers1

2

// 1. with async await
function runKnex(){
        //Run Queries and send Content
        var result = knex.select().table('User')
         return result.then(function(rows){
            return rows;
        })
}

//calling function
mainWindow.webContents.on('did-finish-load',async ()=>{
        const knexres = await runKnex();
        console.log(knexres);
});

// 2. 

function runKnex(){
        //Run Queries and send Content
        
       return  knex.select().table('User').then()
       
}

//calling function
mainWindow.webContents.on('did-finish-load', ()=>{
      runKnex().then(knexres=>{
      
         console.log(knexres);
      
      });
});
mpc
  • 166
  • 6