Wednesday, 31 May 2017

Nodejs calling controller from controller give TypeError: res.json is not a function

getData receives the data fine, but it cannot return anything.

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

Here is my calling function callGetData

var myController = require('./myController');
var callGetData = function() {
    return new Promise((resolve, reject) => {
    myController.getPrice (
    { "test": "TestData" }
    ,function(err, data) {
        if (!err) {
            console.log("Result:" + JSON.stringify(data, null, 4));
            resolve(data);
        } else {
            reject(new Error('ERR: ' + err));
        };
    });
})};

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

Here is the controller that I want to call getData

exports.getData = function(req, res){
    console.log("Received: " + JSON.stringify(req.body, null, 4));
    res.json({error:false,test:"hello from getData"});
}

And here is the error I get:

ERR: TypeError: res.json is not a function



via torbenrudgaard

No comments:

Post a Comment