Tuesday 30 May 2017

Calling node controller from node controller, how to send the data?

I have a priceController node which is a lot of code called by angular in many places.

Now I have another node called frontpageController which would like to call the priceController directly, instead having to go to angular to do it.

But I cannot figure out how to feed priceController with the data the correct way (from angular its easy cause its http post).

How do I call priceController from frontpageController and send the data which it needs (propertyID)?

This is priceController

var mongoose = require('mongoose');
exports.getPrice = function(req, res) {
    console.log("priceController received: " + JSON.stringify(req.body, null, 4));
    console.log("propertyID: " +req.body.propertyID);
    ...lots of code...
    res.json({error:false,priceFindResult})
    console.log("Sent data back to caller");
}

And this is frontpageController

var mongoose = require('mongoose');
exports.getFrontpage = function(req, res) {

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

The result in my node console is as follows

=====START callPriceController=====
priceController received: undefined
getFrontpage ERR: TypeError: Cannot read property 'propertyID' of undefined

So it looks like I actually call the priceController but does not manage to get the propertyID sent there.

How can I do this?



via torbenrudgaard

No comments:

Post a Comment