0

How come it cannot see the res part of my calling function?

Here is my calling function frontpageController.js

exports.getFrontpage = function(req, res) {

    var priceController = require('./priceController');
    var priceModel = require('../models/priceModel');
    var priceTable = mongoose.model('priceModel');
    var callPriceController = function() {
        return new Promise((resolve, reject) => {
        priceController.getPrice (
        { "body": { "propertyID": "WAT-606" } }
        ,function(err, data) {
                console.log("HELLO!!! ");
            if (!err) {
                console.log("Result: " + JSON.stringify(data, null, 4));
            } else {
                console.log("ERR: " + err);
            };
        });
    })};

    callPriceController()
        .catch(err => {
            console.log("getFrontpage ERR: " + err);
            //res.json({error:true,err});
        })
    }

Here is the controller that I want to call getData

exports.getPrice = function(req, res){

    // 
    // Validate the data we get from router
    // 
    console.log("priceController received: " + JSON.stringify(req.body, null, 4));
    res.json({error:false,test:"hello from getPrice"});
}

And here is the console log:

GET /frontpageRoute/getFrontpage - - ms - -
priceController received: {
    "propertyID": "WAT-606"
}
getFrontpage ERR: TypeError: res.json is not a function

Here is the getPrice router:

var express = require('express');
var router = express.Router();
var priceController = require('../controllers/priceController');

router.post('/getPrice', function(req, res) {
    priceController.getPrice(req, res);
});

module.exports = router;

Here is the frontpage router

var express = require('express');
var router = express.Router();
var frontpageController = require('../controllers/frontpageController');

router.get('/getFrontpage', function(req, res) {
    frontpageController.getFrontpage(req, res);
});

module.exports = router;
6
  • what does console.log(res) output? Commented May 31, 2017 at 11:59
  • What do you want to do? Commented May 31, 2017 at 12:00
  • I want to call getData and get the result {error:false,test:"hello from getData"}back Commented May 31, 2017 at 12:01
  • @JeremyThille I never get a result - it jumps directly to the catch and gives the error TypeError: res.json is not a function when it reach the res.json({error:false,test:"hello from getData"}); Commented May 31, 2017 at 12:03
  • But your controller doesn't call getData, it calls getPrice... What am I missing? Commented May 31, 2017 at 12:12

3 Answers 3

1

Since the getPrice(req, res) expects reqand res, you have to pass it when you call getPrice. Update your frontpageController.js to this and see if it works -

exports.getFrontpage = function(req, res) {

var priceController = require('./priceController');
var priceModel = require('../models/priceModel');
var priceTable = mongoose.model('priceModel');
var callPriceController = function() {
    return new Promise((resolve, reject) => {
    priceController.getPrice ({ "body": { "propertyID": "WAT-606" } }, res,
    ,function(err, data) {
            console.log("HELLO!!! ");
        if (!err) {
            console.log("Result: " + JSON.stringify(data, null, 4));
        } else {
            console.log("ERR: " + err);
        };
    });
})};

callPriceController()
    .catch(err => {
        console.log("getFrontpage ERR: " + err);
        //res.json({error:true,err});
    })
}
Sign up to request clarification or add additional context in comments.

9 Comments

YES!!!!! Ohh yes yes yes!! That was it!! - So simple yet so not logical in my head for some reason :-D Ohhh thank you SO MUCH Manjo!! I spent 8 hours on this and all it took was req, res, - and it makes sense actually when I see it now.
I was working on MEAN stack last year on a rental project, so it was easy to figure out this one, happy to help.. :)
That's what I was trying to say......... You were not passing the correct req and res, but an object and a function
@JeremyThille yes and thank you so much for helping me as well!!
I'm frustrated because I was so close :)
|
0
callGetData()
.catch(err => {
    console.log("ERR: " + err);
    res.json({error:true,err})
}

res is not defined in this scope that's why you are getting this error

9 Comments

Actually it is, I just cut all the rest of the code away. The callGetDatacontroller starts with a exports.getMyData = function(req, res) {
@Vikash I'm not sure about that. If res was undefined, the error would be res is undefinedor Cannot resolve function json of undefined.
@JeremyThille exactly and the error is from the getData function, not from the caller.
@JeremyThille it is almost like the res part is not parsed from the caller to the getData. Only the req part is parsed.
I believe I understand what's going on, but first please confirm that you are mistakenly calling myController.getPrice instead of, as you state in your question, myController.getData?
|
0

I believe I understand what's going on here.

You're calling myController.getData, right? However, this function is defined as such :

exports.getData = function(req, res){ }

So it's expecting two arguments : req and res.

But look what you're passing to it : the first argument is { "test": "TestData" }and the second is function(err, data) {...}

So, in getData :

req == { "test": "TestData" }
res == function(req, res){...}

So, res is defined, but absolutely not what you're expecting it to be. And indeed, res.json is not a function.

I think getPrice should be defined this way :

exports.getPrice = function(req, callback){       
    callback( null, {error:false, test:"hello from getPrice"});
}

So now, back to your promise, you get err==null and the correct data.

7 Comments

Ouuuu!!!! Thats right!!!... Ok let me update the question with the real code, cause the error is not in the place I thought it was. But I think you are on to something there. Now I just need to find out how to fix it.
can you have a look now? This is the original code. And the error is in the caller, so it probably have something to do with what you say. I just dont know how to make the call correctly.
Basically it's still the same problem. You're still passing function(err, data) { } as res. But actually, why do you need res at all in getData? That's not where you reply to the browser, is it? Don't you just need to return(null, data) instead of replying with res? I'm lost in your code :) –
I got a lot of nodejs controllers who get different things from MongoDB. So Instead of having Angular call the getPrice controller 20 times, I wanted to have one nodejs controller that call getPrice 20 times and then returns the entire result. And it kind of works, getPrice gets the data but I just cant get it back.
I tried your getPrice example but get a "req is not defined" error.
|

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.