Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

369
Visualizações
How to get data from multiple model in single controller in NodeJS?

I am working on nodejs and i am using "Express js" framework, I am working on Controller and i am trying to get data from "two model functions" but i am getting message "anonymous", How can i get data from multiple "model functions", Here is my code,

  • This is my homeController
homeController.index = function (req, res, next) {
  // fetching data into 'data variable' from FetchRecords model
  homeModel.FetchRecords(function (err, data) {
    if (err) {
      req.flash('error', 'There was no records.');
    } else {
      res.render('home/index', { data: data });
    }
  });

  homeModel.getAverage(function (average) {
    console.log(average);
    // res.render('home/index',{data:average});
  });
};
  • This is my homeMOdel
homeModel.FetchRecords = function (data) {
  sql.query('SELECT * FROM rating', function (err, res) {
    if (err) {
      return data(err, null);
    } else {
      return data(null, res);
    }
  });
};

homeModel.getAverage = function (average) {
  console.log(average);
  sql.query('SELECT avg(value) FROM rating', function (err, res) {
    if (err) {
      return average(err, null);
    } else {
      return average(null, res);
    }
  });
};
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Inside homeModel just create 1 function instead of 2 separate. You can combine both MySQL queries into one like this.

const FetchRecordsAndAverage = function (data) {
      sql.query('SELECT * FROM rating; SELECT avg(value) FROM rating', function (err, res) {
        if (err) {
          return data(err, null);
        } else {
          return data(null, res);
        }
      });
    };

module.exports = { 
    FetchRecordsAndAverage
}

With this you will get combined data of both queries as arrays inside array. Result of queries can be accessed as data[0] & data[1].

over 4 years ago · Santiago Trujillo Relatório

0

You should export the function from the home model:

const FetchRecords = function (data) {
    sql.query('SELECT * FROM rating', function (err, res) {
      if (err) {
        return data(err, null);
      } else {
        return data(null, res);
      }
    });
  };
  
const getAverage = function (average) {
    console.log(average);
    sql.query('SELECT avg(value) FROM rating', function (err, res) {
      if (err) {
        return average(err, null);
      } else {
        return average(null, res);
      }
    });
  };

module.exports = { 
    FetchRecords,
    getAverage
}

And retrieve them in your application by calling:

const { FetchRecords, getAverage } = require('./path/to/home_model');
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda