3

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.

3 Answers 3

5

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
  })
}

Sign up to request clarification or add additional context in comments.

Comments

3

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;
}

Comments

1

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
   });

2 Comments

What if I need to process the results of say two independent queries? Will I need to daisy chain the queries within the thens i.e 2nd query happens within the second then?
There is no need to process 2 truly independent queries in the same Promise chain, as long as it is OK for the 2 queries to be processed asynchronously with respect to each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.