0

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;
    }
 }
3
  • 1
    Do you have a @ResponseBody on your Spring MVC controller method? Commented Jan 4, 2015 at 19:39
  • What do you get by calling the Spring MVC method without using angular? Say using the browser or a Rest utility? Commented Jan 4, 2015 at 19:41
  • Check your HTTP request please. You request to app/rest/chartConfigs/getById and wait on /rest/getById Commented Jan 4, 2015 at 19:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.