Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
Sending multiple arrays from NODEJS API in response not wait for the response

I have tried different ways to send data in arrays but it shows null. I am sure this is because the response fires before the actual response return. Well, this is my guess! I may be wrong.. I want to know the best practice to do this?

My expected result in the payload:

data: {
allCountries: [{TotalCountries: 12}]
allStates: [{StateId: "15327", STR: "Form",…}, {StateId: "15326", STR: "Form",…},…]
AllCities: [,…]
AllCust: {Id: "1825",…}
}

Now, in nodejs controller, I have 4 functions

exports.getAllDetails = async (req, res) => {
  
  if (!req.query.clientId) {
    return res.status(406).send({
      success: false,
      message: "ID is required"
    })
  }

  let id = req.query['custId'];
  let allCountries= await getAllCountries(req, res, id)
  // let allStates= this.getStates(req, res, id);
  // let allCities= this.getAllCities(req, res, id);
  // let custDetails= this.getCustDetails(req, res, id);


  return res.send({
    success: true,
    data:
    [allCountries]
    [allStates],
    [AllCities],
    [AllCust]
  })
}

Now I have created separate functions for all. i.e.

async function getAllCountries(req, res, id) {

  let allCountries;

  allCountries= `SELECT query..`

  connection.query(allCountries, (err, result) => {

    if (result) {
      if (result.length > 0) {
        return result;
      } else {
        res.status(204).send({
          success: false,
          message: `No data found.`,
        });
      }
    }
  });
}

I am getting null array in result? Can anyone tell me the best way to do this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Because you're trying to return data from callback function :

async function getAllCountries(req, res, id) {

  let allCountries;

  allCountries= `SELECT query..`

  connection.query(allCountries, (err, result) => {

    if (result) {
      if (result.length > 0) {
        return result; // this won't work because it is inside callback function.
      } else {
        res.status(204).send({
          success: false,
          message: `No data found.`,
        });
      }
    }
  });
}

you need to return promise object :

async function getAllCountries(id) {

  let allCountries;

  allCountries= `SELECT query..`

  return new Promise((resolve) => {
    connection.query(allCountries, (err, result) => {

    if (result) {
      if (result.length > 0) {
        resolve(result);
      } else {
        resolve(null);
      }
    }
    });
  });
}

also where you're using this function :

  let allCountries= await getAllCountries(id);
  if (allCountries == null) {
      return res.status(204).send({
         success: false,
         message: `No data found.`,
      });
  }
about 4 years ago · Juan Pablo Isaza Report

0

I Will recommend you start using try/catch to handle errors instead of all your function passing the param req/res, whit this solution your code will be more readable.

function getAllCountries(req, res, id) {
  return new Promise((res, rej) => {
    let allCountries;
    allCountries = `SELECT query..`;
    connection.query(allCountries, (err, result) => {
      if (err) {
        rej(err);
      }

      if (result.length === 0) {
        rej({
          success: false,
          message: `No data found.`,
        });
      }
      res(result);
    });
  });
}

in your main function:

exports.getAllDetails = async (req, res) => {
  if (!req.query.clientId) {
    return res.status(406).send({
      success: false,
      message: 'ID is required',
    });
  }

  try {
    let id = req.query['custId'];
    let allCountries= await getAllCountries(id)
    ...
  
  
    return res.send({
      success: true,
      data: {
        allCountries, //as you want this as array wrap the allCountries as [allCountries]
        ...
      }
    })
  } catch (err) {
    return res.status(400).send(err);
  }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!