javascript - could not get the property value of my object in angularJs -


i undefined whenever value of property of object.

function run(id){    var report = services.getreportinfo(id);     var childreport = {        id: newguid(),        parentid: report.id, // undefined        reportpath: report.path  // 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    }; } 

whenever put breakpoint on var report = services.getreportinfo(id) contains correct values each property of report object. however, when report.id or report.path, undefined value.

--edited--

oh, know got wrong.

the getreportinfo function returns array , i'm accessing properties without telling index should values said properties.

function run(id){     var report = services.getreportinfo(id);      var childreport = {        id: newguid(),        parentid: report[0].id,        reportpath: report[0].path      };      ... } 

i placed static index 0, since know array have length of 1.

you not returning .factory method , getreportinfo not returning anything. trying do, try use .service method:

angular.module('project.services').service('services', function(){     var reports = [       {          ....       },       {          ....       }    ];     this.getreportinfo = function (id){       var report = reports.filter(function(element){           return element.id === id;       });       return report;    } } 

here explanation on how use .factory , .service:
confused service vs factory


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -