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

517
Views
How can we call 2 models in one controller in Node Express JS...?

I am new in Nodejs and I am using express JS framework with MVC pattern I want to get data from two different tables (with use of two different functions in model) in single controller function, Can I do this ?

  1. How can I pass parameters to two different model
  2. How can we manage result/response from two different model ?

-- Here is my Model

sensorModel.getAllSensorSite=function(result){
    sql.query("SELECT * FROM site",function(err,res){
        if(err) {
            return result(err,null);
        }
        else{
         return result(null,res);
        }
    });
}

sensorModel.getAllSensorRowss=function(result){
    sql.query("SELECT * FROM rowss",function(err,res){
        if(err) {
            return result(err,null);
        }
        else{
         return result(null,res);
        }
    });
}

-- Here is my Controller

sensorController.index=function(req,res,next){
    sensorModel.getAllSensorSite(function(err,site){
        if(err){
                throw err;
        }else{
            res.json({ site });
        }       
    });
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Using this method, you're sending queries to database but you can't wait for their reply. I would recommend to use bluebird to be able to wait for database responses or to use an ORM (like sequelize) along with await/async

over 4 years ago · Santiago Trujillo 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!