1

I'm trying to create a web server, and I'm using to MVC model so I tried to use routes inside so I don't know how can I do this. in the console log thats return all data otherwise in postman I test it it doesn't works. here is my code.

AirModel.js :

AirMonitoring.getAllData = (result) =>{
    db.query('SELECT * FROM AirMonitoring', (err, res)=>{
        if(err){
            console.log('Error while fetching airMonitoring', err);
            result(null,err);
        }else{
            console.log('AirMonitoring fetched successfully');
            result(null,res);
        }
    })
}

airController.js :

exports.getAllData = (req, res)=> {
        AirModel.getAllData((err, airMonitoring) =>{
            if(err)
            res.send(err);
            console.log('data', airMonitoring);
            res.send(airMonitoring)
        })
    }

index.js :

const server = http.createServer(function(req, res) {
console.log("http was created!");

  if(req.url == '/airMonitoring'){
    res.writeHead(200, { 'Content-Type': 'application/json' });
      // get latest record of airMonitoring 
     router.get('/airMonitoring', airController.getAllData);
    
    res.end();  
}
});
5
  • I'm a little confused regarding your code... In the actual db.query, you're not doing anything with the data other than logging it to the console. Why would you expect anything otherwise? Commented Oct 28, 2021 at 13:14
  • Yeah I'm connecting with raspberry db, and with the AirModel there is a query getting the data from db so nothing return from the controller I used in routes, why?! Commented Oct 28, 2021 at 13:19
  • I'm assuming the db.connect code is in your AirModel?? If so, look at the code, you're not returning any of the data, or even assigning it for that matter. You're just logging it to the console. That's why it's not returning anything... Show a little more of your code the surrounds the db.connect bit, so I can be sure this is what's happening, and I can give you a working example. Commented Oct 28, 2021 at 13:23
  • I updated my question you can check it now Commented Oct 28, 2021 at 13:32
  • Okay that makes more sense. Checking it out now. Commented Oct 28, 2021 at 13:43

1 Answer 1

1

It's not very clear what router is but I'm assuming it's an express router, and that's not how routing works. Currently you are (re?)defining the route on each request. The routing page is a good place to start, but basically you need to define the routes once.

var express = require('express')
var app = express()

app.get('/airMonitoring', airController.getAllData);

app.listen(PORT, () => {
  console.log(`Example app listening at http://localhost:${PORT}`)
})

And also in your AirModel.js you have an error as far as I can tell, when handling the database error you should provide it as first argument, not second:

result(null,err); /* has to be result(err, null) */
Sign up to request clarification or add additional context in comments.

3 Comments

This is almost certainly the problem. I looked right passed the fact that the route was nested in an if...
Not only is it within the request handler, but also nothing is using the router, so even if it were defined properly it would still not make a difference.
Unless that code was left out of the OP, like much of the code was to begin with. But yes, you are right.

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.