如何执行该函数将值返回到节点js文件中的被调用函数

时间:2013-07-29 14:36:21

标签: javascript node.js function callback

我有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);
    };
    
  4. 面临的问题

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

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

    3. 非常感谢任何指导。

1 个答案:

答案 0 :(得分:0)

  

无法将subscriptionRestService中的变量resourceIdentifier发送到mysqlRestService.js

嗯,你没发送它。它当前是调用中回调函数的参数,而不是getPricingDetails参数的参数。使用

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

无法将pricingdetail从mysqlRestService.js返回到subscriptionRestService中的调用函数。

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

callback(pricingDetail);