私は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); }
REST API 呼び出しを処理する subscriptionRestService.js:
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に変数を送信できませんpricingdetail
mysqlRestService.js から subscriptionRestServiceの呼び出し関数に戻すことができません。
どんなガイダンスでも大歓迎です。