ali ibrahim
ali ibrahim

Reputation: 95

AngularJS object

I'm newbie in Angularjs,The following Spring controller get object from database I want to print this object attributes in angularjs controller but I get undefined

  @RequestMapping(value = "/rest/getById",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RolesAllowed(AuthoritiesConstants.ADMIN)
    public ChartConfigs findOne(Integer id) {
         return chartConfigService.getOne(1);

    }

This is Angularjs service

    myappApp.factory('ChartConfigService', function ($http) {
 return {
    findOne: function() {
        var promise = $http.get('app/rest/chartConfigs/getById').then(function (response) {
            return response.data;
        });
        return promise;
    }
   }
  });

This is Angularjs controller

  myappApp.controller('ChartConfigController', function ($scope, ChartConfigService) {
        $scope.message = ChartConfigService.findOne();
        var obj=ChartConfigService.findOne();
            console.log(obj.type);



  });

chartconfig domain

package com.innvo.domain;

    @Entity
    @Table(name = "T_CHART_CONFIGS")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class ChartConfigs {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private Integer id;

    @Column(name = "category")
    private String category;

    @Column(name = "type")
    private String type;



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
 }

Upvotes: 0

Views: 67

Answers (1)

Marcel Gwerder
Marcel Gwerder

Reputation: 8520

In your case obj will be a promise too so you have to do it like:

ChartConfigService.findOne().then(function(obj) {
    console.log(obj.type);
});

obj.type is undefined because type does not exist on the promise object.

Upvotes: 1

Related Questions