1

undefinedオブジェクトのプロパティの値を取得するたびに取得します。

function run(id){
   var report = services.getReportInfo(id);

   var childReport = {
       id: newGuid(),
       parentId: report.id, // i get undefined
       reportPath: report.path  // i get undefined
   };

   ...
}

services.js

angular.module('project.services').factory('services', function(){

   var reports = [
      {
         ....
      },
      {
         ....
      }
   ];

   function getReportInfo(id){
      var report = reports.filter(function(element){
          return element.id === id;
      });
   };

   return{
      getReportInfo: getReportInfo
   };
}

ブレークポイントを配置var report = services.getReportInfo(id)すると、レポート オブジェクトの各プロパティの正しい値が含まれる可能性があります。ただし、report.id または report.path を取得すると、未定義の値が取得されます。

--編集済み--

ああ、私はどこで間違っていたかを今知っています。

getReportInfo 関数は配列を返し、どのインデックスからそのプロパティの値を取得する必要があるかを知らずにプロパティにアクセスしています。

function run(id){
    var report = services.getReportInfo(id);

    var childReport = {
       id: newGuid(),
       parentId: report[0].id,
       reportPath: report[0].path 
    };

    ...
}

配列の長さは常に 1 になることがわかっているため、静的インデックス 0 を配置しました。

4

2 に答える 2