Javascript 从节点控制器调用节点控制器,如何发送数据?

Javascript 从节点控制器调用节点控制器,如何发送数据?,javascript,node.js,Javascript,Node.js,我有一个priceController节点,它是angular在许多地方调用的大量代码 现在我有另一个名为frontpageController的节点,它想直接调用priceController,而不是必须转到angular来执行此操作 但是我不知道如何以正确的方式向priceController提供数据(因为它很容易导致它的httppost) 如何从frontpageController调用priceController并发送所需的数据(propertyID) 这是价格控制器 var mong

我有一个priceController节点,它是angular在许多地方调用的大量代码

现在我有另一个名为frontpageController的节点,它想直接调用priceController,而不是必须转到angular来执行此操作

但是我不知道如何以正确的方式向priceController提供数据(因为它很容易导致它的httppost)

如何从frontpageController调用priceController并发送所需的数据(propertyID)

这是价格控制器

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");
}
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));
            };
        });
    })};
这是frontpageController

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");
}
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));
            };
        });
    })};
我的节点控制台中的结果如下

=====START callPriceController=====
priceController received: undefined
getFrontpage ERR: TypeError: Cannot read property 'propertyID' of undefined
因此,看起来我实际上调用了priceController,但没有将propertyID发送到那里


我怎么能做到这一点呢?

我不能确切地告诉您这里要做什么,但是函数
getPrice
接受参数req和res,它们可能是请求和响应对象。当您调用
getPrice
时,您正在向它传递一个数组和一个函数。由于数组没有属性
body
req.body
未定义,因此尝试访问
req.body
的任何属性(在本例中为
propertyId
)将抛出类型错误。但是,即使您超过了这一点,当您尝试调用
res.json
时,它也会出错,因为您正在向它传递一个没有该方法的函数


也许您可以将req和res对象传递给
getPrice

Ahh是的-您是对的,我必须解析主体。你会怎么做?通过这样做使它工作:
priceController.getPrice({“body”:{“propertyID”:“WAT-606”}
-但是现在priceController在尝试返回数据
res.json({error:false,priceFindResult})时抱怨没有“res”