简体   繁体   中英

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?

I can't tell exactly what you're going for here but the function getPrice takes the arguments req and res which are presumably supposed to be the request and response objects. When you call getPrice you're passing it an array and a function. Since the array doesn't have a property body , req.body is undefined so trying to access any property of req.body (in this case propertyId ) is going to throw a TypeError. Even if you got past that point, though, it would error later when you tried to call res.json because you're passing it a function that doesn't have that method.

Maybe you could pass the req and res objects to getPrice ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM