user275157
user275157

Reputation: 1352

NodeJS Sequelize returning data from query

Totally new to Javascript and Node. I am trying to get started with Sequelize as an ORM and did a simple query

var employeeList = Employee.findAll().then(function(result){
    console.log(result.length);
    console.log(result[0].employeeName);
    //how do I return this to a variable with which I can do further processing
    return result;
 });

//do something with employeeList
employeeList[0].employeeName //cannot read property of undefined 

While the console logs print out the right name the employeeList itself does not contain any data. I tried printing the employeeList and it shows the promise

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

I did skim through the promise concept but could not get an east example as to how to return the results from the promise to a variable outside the function. I thought returning the result would do t he trick. Am I missing something here? I can understand that I could work on the results within the promise function. If the scenario is to make two database calls and then process the results of both calls to return a merged result how could that be done without getting results to variables.

Upvotes: 3

Views: 4999

Answers (3)

AlexScr
AlexScr

Reputation: 452

so based on your post on comments of using to independent queries I'd like to show you how to use them properly:

//Each request in it's own function to respect the single responsability principle 
function getAllEmployees() {
  return Employee
    .findAll()
    .then(function(employees){
      //do some parsing/editing
      //this then is not required if you don't want to change anything
      return employees;
     });
}

function doAnotherJob() {
  return YourModel
    .findAll()
    .then(function(response) => {
      //do some parsing/editing
      //this then is not required if you don't want to change anything
      return response;
    });
}

function do2IndependentCalls() {
  return Promise.all([
    getAllEmployees(),
    doAnotherJob()
  ]).then(([employees, secondRequest]) => {
    //do the functionality you need
  })
}

Upvotes: 5

Vivek Doshi
Vivek Doshi

Reputation: 58533

Another way of doing is use async await :

async function getEmployees(){
    var employeeList = await Employee.findAll().then(function(result){
        console.log(result.length);
        console.log(result[0].employeeName);
        return result;
    });

    employeeList[0].employeeName;
}

Upvotes: 3

cybersam
cybersam

Reputation: 66967

The then method returns a Promise, not your employee list.

If you want to use the employee list, you should either do that in the function passed to your existing then call, or in a subsequent then call, as shown below.

Employee
  .findAll()
  .then(function(el){
    console.log(el.length);
    console.log(el[0].employeeName);
    return el;
   })
  .then(function(employeeList){
    //do something with employeeList
    employeeList[0].employeeName
   });

Upvotes: 1

Related Questions