如何调试角度for循环

时间:2015-05-14 01:56:09

标签: angularjs

我正在尝试批量上传多个pdf。每个文件都以特定格式命名

  

02-2015 HIP BS32

日期/管道缩写/位置缩写。

我通过循环运行文件,将缩写与数据库中的列表进行比较。然后,我将这些属性分配给对象,然后再将其传递给API控制器。管道和位置表的设置完全相同。我遇到的问题是管道问题。 这就是问题所在,

$scope.pip = $scope.pipeLookup[matches[1]];
$scope.loc = $scope.locationLookup[matches[2]];

locationLookup与Location Abbreviation匹配并将其放在$ scope.loc变量中。但是pipeLookup不起作用。它当然是抛出错误,因为管道属性是未定义的。我不确定我是不是只是在寻找一些东西。如果需要更多信息,请告诉我 pic1 pic2

 $scope.companies = Company.query(function () { });
$scope.locations = Location.query(function () { });
$scope.pipes = Pipe.query(function () { });
$scope.selectCompany = function () {
    var id = $scope.companyId.CompanyId
    $http.get('/api/apiCompany/' + id)
        .success(function (result) {
            $scope.CompanyName = result.CompanyName
        });
};//

$scope.pipeLookup = {};
$scope.locationLookup = {};

$scope.upload = function () {
    var files = $scope.files;
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];

            $scope.pipes.map(function (pip) {
                $scope.pipeLookup[pip['PipeAb']] = pip;
            });
            $scope.locations.map(function (loc) {
                $scope.locationLookup[loc['LocationAb']] = loc;
            });

            var matches = file.name.match(/^\d+\D\d+\s*?(\S*)\s*(\S*?)\./i);

            $scope.pip = $scope.pipeLookup[matches[1]];
            $scope.loc = $scope.locationLookup[matches[2]];

            $upload.upload({
                url: '/api/apiBatchPipeLine',
                fields: {
                    'typeId': 1,
                    'companyId': $scope.companyId.CompanyId,
                    'documentDate': $scope.model.documentDate,
                    'pipeId': $scope.pip['PipeId'],
                    'pipeName': $scope.pip['PipeName'],
                    'locationId': $scope.loc['LocationId'],
                    'locationAb': $scope.loc['LocationAb']
                },
                file: file
            }).progress(function (evt) {
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
            }).success(function (data, status, headers, config) {
                console.log('file ' + config.file.name + 'uploaded. Response: ' +
                            JSON.stringify(data));
            }).error(function (err, result) {
                console.log(err, result);
            });

        }
    }

$ scope.pipeLookup JSON

{"ANR ":{"$id":"1","PipeId":1,"PipeAb":"ANR ","PipeName":"Transcanada ANR Pipeline","Documents":null},"CGT ":{"$id":"2","PipeId":2,"PipeAb":"CGT ","PipeName":"Columbia Gulf Transmission","Documents":null},"CHA ":{"$id":"3","PipeId":3,"PipeAb":"CHA ","PipeName":"Enterprise Channel Pipeline","Documents":null},"FGT ":{"$id":"4","PipeId":4,"PipeAb":"FGT ","PipeName":"Energy Transfer Florida Gas Transmission","Documents":null},"GAB ":{"$id":"5","PipeId":5,"PipeAb":"GAB ","PipeName":"Enbridge Garden Banks Pipeline","Documents":null},"GLO ":{"$id":"6","PipeId":6,"PipeAb":"GLO ","PipeName":"American Midstream Gloria  Pipeline","Documents":null},"GSO ":{"$id":"7","PipeId":7,"PipeAb":"GSO ","PipeName":"Boardwalk Gulf South Pipeline","Documents":null},"HIO ":{"$id":"8","PipeId":8,"PipeAb":"HIO ","PipeName":"Enterprise High Island Offshore System","Documents":null},"HIP ":{"$id":"9","PipeId":9,"PipeAb":"HIP ","PipeName":"American Midstream High Point Pipeline","Documents":null},"HPL ":{"$id":"10","PipeId":10,"PipeAb":"HPL ","PipeName":"Energy Transfer Houston Pipeline","Documents":null},"TEJ ":{"$id":"11","PipeId":11,"PipeAb":"TEJ ","PipeName":"Kinder Morgan Tejas Pipeline","Documents":null},"KIN ":{"$id":"12","PipeId":12,"PipeAb":"KIN ","PipeName":"Kinetica Partners Pipeline","Documents":null},"NGP ":{"$id":"13","PipeId":13,"PipeAb":"NGP ","PipeName":" Kinder Morgan Natural Gas Pipeline","Documents":null},"SER ":{"$id":"14","PipeId":14,"PipeAb":"SER ","PipeName":" Energy Transfer Sea Robin Pipeline Company ","Documents":null},"SNT ":{"$id":"15","PipeId":15,"PipeAb":"SNT ","PipeName":"Kinder Morgan Southern Natural Gas Company","Documents":null},"SRY ":{"$id":"16","PipeId":16,"PipeAb":"SRY ","PipeName":"MCPO Stingray Pipeline Company","Documents":null},"TGT ":{"$id":"17","PipeId":17,"PipeAb":"TGT ","PipeName":"Kinder Morgan Tennessee Gas Pipeline","Documents":null},"TET ":{"$id":"18","PipeId":18,"PipeAb":"TET ","PipeName":"Spectra Energy Texas Eastern Pipeline","Documents":null},"TXG ":{"$id":"19","PipeId":19,"PipeAb":"TXG ","PipeName":"Boardwalk Texas Gas Transmission","Documents":null},"TSC ":{"$id":"20","PipeId":20,"PipeAb":"TSC ","PipeName":"Williams Transcontinental Gas Pipeline","Documents":null},"TRK ":{"$id":"21","PipeId":21,"PipeAb":"TRK ","PipeName":"Energy Transfer Trunkline Gas Company","Documents":null},"VGS ":{"$id":"22","PipeId":22,"PipeAb":"VGS ","PipeName":"Targa Venice Gathering System","Documents":null}}

$ scope.locationLookup Json

{"VR16":{"$id":"1","LocationId":1,"LocationAb":"VR16","LocationName":"Location VR16","Documents":null},"MP140\t":{"$id":"2","LocationId":2,"LocationAb":"MP140\t","LocationName":"Location MP140","Documents":null},"BS32":{"$id":"3","LocationId":3,"LocationAb":"BS32","LocationName":"Location BS32","Documents":null},"MP46":{"$id":"4","LocationId":4,"LocationAb":"MP46","LocationName":"Location MP46","Documents":null},"DF78":{"$id":"5","LocationId":5,"LocationAb":"DF78","LocationName":"Location DF78","Documents":null},"UR56":{"$id":"6","LocationId":6,"LocationAb":"UR56","LocationName":"Location UR56","Documents":null}}

1 个答案:

答案 0 :(得分:2)

你的正则表达式期望结尾有一个点(。)字符,但你的字符串末尾不包含一个点(。),因此它失败了。

如果我尝试使用正则表达式^\d+\D\d+\s*?(\S*)\s*(\S*?)\. here并传入02-2015 HIP BS32,那么它就不匹配,因为你的正则表达式最终期望.字面值。< / p>

更新:这是pipeLookup JSON的片段

"HIP ": {
"$id": "9",
"PipeId": 9,
"PipeAb": "HIP ",
"PipeName": "American Midstream High Point Pipeline",
"Documents": null

}

这里的关键是&#34; HIP&#34;注意HIP之后的额外空格。这就是$scope.pipeLookup[matches[1]]什么都不返回的原因。在将其存储在pipeLookup对象

中之前删除该尾随空格

更新:仔细看看,如果file.name包含此值02-2015 HIP BS32,并且您希望获得HIP值,则可以使用以下正则表达式

/^\d+\D\d+\s*(\S*\s*)(\S*)/i

以下是此正则表达式示例的jsbin live demo