Javascript:使用回调时函数参数变得不确定

时间:2019-04-23 19:54:29

标签: javascript

我有一个函数,可以接收一些数据,将其组织成一个数组并返回它。效果很好,但是后来我决定,如果将building id变量转换为正式名称会更好。因此,我尝试使用一个简单的回调,但是由于某种原因,该函数采用的参数变得不确定,因此我的代码开始中断。我收到以下错误,无法读取未定义的属性“ Building_Level”

convertBuildingsArray: function(data){
    var maxBuildings = 5; //This is the max number of buildings for each province.  
    var buildingArray = [];
    var dummyArray = [];

    for(var i=0; i<maxBuildings; i++){
        if(typeof data[i] == 'undefined'){
            buildingArray.push('Empty');
        } else{
                module.exports.getBuildingName(data[i].FK_BuildingId, function(response){
                    dummyArray.push(response, data[i].Building_Level, data[i].Location); //Error occurs here
                    var x = dummyArray;
                    buildingArray.push(x);
                });

        }
    }
    return buildingArray;
},


//This function recieves a building id and returns its name (Works)
getBuildingName: function(buildingId, callback){
        var sql = "SELECT Building_Name from buildings WHERE BuildingId = (?)";
        var value = [buildingId];
        db.query(sql, value, function(err,result,fields){
            if(err){
                console.log(err);
            } else{
                return callback(result[0].Building_Name);
            }
        });
},

0 个答案:

没有答案