我有 3 个节点 js 文件:
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); }
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){ }); });
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); };
面临的问题
无法将变量
resourceIdentifiers
从 subscriptionRestService 发送到 mysqlRestService.js无法从 mysqlRestService.js 返回到
pricingdetail
subscriptionRestService 中的调用函数。
非常感谢任何指导。