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