如何从SP中检索的函数返回值?

时间:2017-06-14 05:15:33

标签: javascript node.js

我有一个函数,我在其中调用SP来检索细节。我的行[0]包含从SP检索的所有数据。我需要将此值作为函数的返回值返回。 但每当我回来时,我都会得到**

  

未定义

**。

如何通过函数返回我的结果。 以下是我的代码:

    function GetBOMData(BOMName, params, notifySchneiderParam, res, cubicleType, cubicle) {   
      var ret; 
      connection.query('CALL ' + BOMName + '(' + params + ')', function (error, rows, result){
       ret= rows[0];    
    });
    return ret;
}

这就是我调用我的功能的方式:

function GetI06TIncomerBOM(cubicleType, body, res, userID, notifySchneiderParam) {

var operatingVoltageUs = body.TechCharacteristics.OperatingVoltage ? parseFloat(body.TechCharacteristics.OperatingVoltage.replace("kV", "")) : 0;
var packingType = body.TechCharacteristics.PackingType;
var cableDeviceSelected = body.cubicleArray[0].grid.CableTestDevice;
var door500MMSelected = body.cubicleArray[0].grid.depthdoor;
var cableType = body.cubicleArray[0].grid.CableType ? body.cubicleArray[0].grid.CableType : null;
var isNotifiedSchneider = notifySchneiderParam ? 1 : 0;

var params = '"' + cubicleType + '",' + operatingVoltageUs + ',"' + packingType + '",' +
    cableDeviceSelected + ',' + door500MMSelected + ',"' + cableType + '",' + userID + ',' + isNotifiedSchneider;

// Sample SP call : call usp_GetI06TIncomerBOM("I06TIncomer",6,"LandPacking",1,1,"3x1C",5,0) 
var Test = GetBOMData('usp_GetI06TIncomerBOM', params, notifySchneiderParam, res, cubicleType, body.cubicleArray[0]);
return res.json(Test);

   }

1 个答案:

答案 0 :(得分:0)

您可以将回调传递给GetBOMData中的GetI06TIncomerBOM函数,如下所示。

你可以检查一下并尝试一下,

function GetBOMData(BOMName, params, notifySchneiderParam, res, cubicleType, cubicle, callback) {   

      connection.query('CALL ' + BOMName + '(' + params + ')', function (error, rows, result){ 
       if(error) {
          callback(error);
       } else {
          callback(null, rows[0]); 
       }   
    });
}

如下所示更改GetI06TIncomerBOM

function GetI06TIncomerBOM(cubicleType, body, res, userID, notifySchneiderParam) {

var operatingVoltageUs = body.TechCharacteristics.OperatingVoltage ? parseFloat(body.TechCharacteristics.OperatingVoltage.replace("kV", "")) : 0;
var packingType = body.TechCharacteristics.PackingType;
var cableDeviceSelected = body.cubicleArray[0].grid.CableTestDevice;
var door500MMSelected = body.cubicleArray[0].grid.depthdoor;
var cableType = body.cubicleArray[0].grid.CableType ? body.cubicleArray[0].grid.CableType : null;
var isNotifiedSchneider = notifySchneiderParam ? 1 : 0;

var params = '"' + cubicleType + '",' + operatingVoltageUs + ',"' + packingType + '",' +
    cableDeviceSelected + ',' + door500MMSelected + ',"' + cableType + '",' + userID + ',' + isNotifiedSchneider;

// Sample SP call : call usp_GetI06TIncomerBOM("I06TIncomer",6,"LandPacking",1,1,"3x1C",5,0) 
var Test = GetBOMData('usp_GetI06TIncomerBOM', params, notifySchneiderParam, res, cubicleType, body.cubicleArray[0], function(err, result) {
    if(err) {
        return res.status(500).send(err);
    } else {
        return res.json(result);
    }
});

}

注意:我也添加了错误处理。