0

我有 3 个节点 js 文件:

  1. mysqlconnection.js 存储数据库连接属性:

    var mysql = require('mysql');
    var cjson = require('cjson');
    var yaml_config = require('node-yaml-config');
    
    // project files
    var config = yaml_config.load(__dirname + '/billingv2.yaml');
    
    
    exports.execute = function(callback){
    
        var connection = mysql.createConnection(
            {
              host     : config.host,
              user     : config.user,
              password : config.password,
              database : config.database,
            }   
        );
        connection.connect();
    
        return callback(null,connection);   
    }
    
  2. subscriptionRestService.js 处理 REST api 调用:

    var express = require('express');
    var app = express();
    
    app.use(express.bodyParser());
    var fs = require('fs');
    // Project files
    var mysql = require('./mysqlRestService.js');
    // Get Resource Subscription data by Resourceuri
    app.post('/pricingdetails', function(req, res) {
    
        var workload = req.body;
        if(workload.elements && workload.elements.length > 0)
        {
            var arr = [];
            for(var index in workload.elements)
            {
                arr[index] = workload.elements[index].uri;
            }
    
            var resourceIdentifiers = arr.join(',');
        }
        console.log(resourceIdentifiers);
        mysql.getPricingDetail(function(resourceIdentifiers,callback){
    
        });
    
    });
    
  3. mysqlRestService.js 处理 mysql 查询/存储过程:

    // packages
    var mysql = require('mysql');
    var cjson = require('cjson');
    var fs = require('fs');
    var yaml_config = require('node-yaml-config');
    
    // project files
    var dbconnection = require('./mysqlconnection');
    exports.getPricingDetail = function (resourceIdentifiers,callback){
        console.log('entered into mysql function');
        console.log(resourceIdentifiers);
        var pricingDetail = {};
        dbconnection.execute(function(err,response){
    
            if(err){
                throw err;
                }
            else 
                { 
    
                    var selectqueryString = "call SP_ExposePricingDetailforUI('" + resourceIdentifiers + "')";
                    response.query(selectqueryString, function(err,pricingDetail){
                    if(err) {   
                        throw err;          
                    }
                    else 
                    {
                        console.log(pricingDetail);
                        pricingDetail = pricingDetail;
    
                    }
                    });
                }
    
        });
    
        //console.log('printing pricing details');
        //console.log(pricingDetail);
    };
    

面临的问题

  1. 无法将变量resourceIdentifiers从 subscriptionRestService 发送到 mysqlRestService.js

  2. 无法从 mysqlRestService.js 返回到pricingdetailsubscriptionRestService 中的调用函数。

非常感谢任何指导。

4

1 回答 1

0

无法将变量 resourceIdentifiers 从 subscriptionRestService 发送到 mysqlRestService.js

好吧,你没有发送它。它目前是调用中回调函数的参数,而不是getPricingDetails. 利用

mysql.getPricingDetail(resourceIdentifiers, function callback(result){
    // use result here
});

无法将 mysqlRestService.js 中的定价详细信息返回到 subscriptionRestService 中的调用函数。

我不知道pricingDetail = pricingDetail;应该做什么。你必须在callback这里调用(调用)!利用

callback(pricingDetail);
于 2013-07-29T14:50:14.927 回答