0

I have a RESTful API . It gives me access to a MYSQL database, where I'm trying to get some data using custom function getScholarshipfindByField and using those JSON data i want to build a page using AngularJS. But I got Error: [$resource:badcfg] Error in resource configuration for action getScholarshipfindByField. Expected response to contain an object but got an array (Request: GET `http://localhost:8080/scholarship/webresources/scholarship.scholarship)

// Service

var Services = angular.module('raServices', ['ngResource']);
Services.factory("Scholarship",function($resource){  
return     $resource('http://localhost:8080/scholarship/webresources/scholarship.scholarshi p/level=:level/field=:field/country=:country/language=:language',      {level:'@level',field:'@field',country:'@country',language:'@language'}, {},{
findAll:{method:'GET', isArray:true},
countREST:{method:'GET', isArray:true},
create:{method:'POST'},
edit:{method:'PUT'},
query: {method:'GET',params:  {level:'@level',field:'@field',country:'@country',language:'@language'},isArray: true},
remove:{method:'DELETE'},
find:{method:'GET',params: {id: 'id'},isArray: false},
findRange:{method:'GET'},
getScholarshipfindByField:{method:'GET',params:{level:'@level',field:'@field',country:'@country',language:'@language'},isArray:false,responseType:'json'}

} )
});

// Controller

 .controller('View2Ctrl',['$scope', 'Scholarship', function($scope, Scholarship) {      
  $scope.data = {};
  $scope.submitForm = function(){
  Scholarship.getScholarshipfindByField(   {'level':$scope.data.level,'field':$scope.data.field,'country':$scope.data.country,'language':$scope.data.language}, function( response ){
  $scope.allscholarship = response;
});
} 

   }]);

//ScholarshipFacadeRest

 @GET
@Path("{level}/{field}/{country}/{language}")
@Produces({ "application/json"}) 
public List<Scholarship> getScholarshipfindByField(@PathParam("level") String level,@PathParam("field") String field,@PathParam("country") String country,@PathParam("language") String language){
        return super.getScholarshipfindByField(level,field,country,language);

}     

// AbstractFacade.java

public List<T> getScholarshipfindByField(Object level,Object field,Object country,Object language) {
  CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
  CriteriaQuery cq = cb.createQuery();
  Root<T> c = cq.from(entityClass);      
  cq.select(c);   
  cq.where(cb.equal(c.get("level"), level),cb.equal(c.get("field"), field),cb.equal(c.get("country"), country),cb.equal(c.get("language"), language));
  return getEntityManager().createQuery(cq).getResultList();

}

//html

    <h3>Choose yours sholarship</h3>
</br></br>
<div ng-app="myApp"   ng-controller="View2Ctrl as data"  >
    <div>         
        <form name="myForm" ng-submit="submitForm()"> 
            <div ng-hide="shows" >
                <div class="form-group">    
                        <label for="level"> Select level of study: </label><br>
                    <select name="level" ng-model="data.level">
                        <option value="All">All</option>
                        <option value="Bachelor">Bachelor</option>
                        <option value="Masters">Master</option>
                        <option value="PhD Degree">PhD Degree</option>
                        <option value="Intership">Intership</option>
                        <option value="Non Degree">Non Degree</option>
                    </select></br></br></br>
                    <tt>{{data.level}}</tt><br/>
                </div>
                <div class="form-group">
                    <label for="field"> Select field of study: </label><br>
                    <select name="field" ng-model="data.field">
                        <option value="All">All</option>
                        <option value="Information Technology">Information Technology</option>
                        <option value="Economics and Finance">Economics and Finance</option>
                        <option value="Business and Management">Business and Management</option>
                        <option value="Engineering">Engineering</option>
                        <option value="Computer Science">Computer Science</option>
                        <option value="Social Sciences">Social Sciences</option>
                        <option value="Physical and Life Sciences">Physical and Life Sciences</option>
                        <option value="Arts">Arts</option>
                        <option value="Humanities">Humanities</option>
                        <option value="Agriculture">Agriculture</option>
                    </select></br></br></br>
                    <tt>{{data.field}}</tt><br/>
                </div>
                <div class="form-group">
                    <label for="country"> Select country of study: </label><br>
                    <select name="country" ng-model="data.country">
                        <option value="All">All</option>
                        <option value="USA">USA</option>
                        <option value="UK">UK</option>
                        <option value="Netherlands">Netherlands</option>
                        <option value="Australia">Australia</option>
                        <option value="Belgium">Belgium</option>
                        <option value="Bulgaria">Bulgaria</option>
                        <option value="Canada">Canada</option>
                        <option value="Czech Republic">Czech Republic</option>
                        <option value="Denmark">Denmark</option>
                        <option value="Finland">Finland</option>
                        <option value="France">France</option>
                        <option value="Germany">Germany</option>
                        <option value="France">France</option>
                        <option value="Greece">Greece</option>
                        <option value="France">France</option>
                        <option value="Italy">Italy</option>
                        <option value="Japan">Japan</option>
                        <option value="Luxembourg">Luxembourg</option>
                        <option value="New Zealand">New Zealand</option>
                        <option value="Norway">Norway</option>
                        <option value="Poland">Poland</option>
                        <option value="Slovakia">Slovakia</option>
                        <option value="Slovenia">Slovenia</option>
                        <option value="Spain">Spain</option>
                        <option value="Sweden">Sweden</option>
                        <option value="Switzerland">Switzerland</option>
                        <option value="Other">Other</option>
                    </select></br></br></br>
                    <tt>{{data.country}}</tt><br/>
                </div>
                <div class="form-group">
                    <label for="language"> Select language of  study: </label><br>
                    <select name="language" ng-model="data.language">
                        <option value="All">All</option>
                        <option value="English">English</option>
                        <option value="German">German</option>
                        <option value="French">French</option>
                    </select></br></br></br>
                    <tt>{{data.language}}</tt><br/>
                </div>
                <button ng-click=" shows = true" type="submit" >Go!</button>
            </div>
        </form>
    </div>
    <div ng-show="shows" >
        <div align="center" div ng-app="myApp" ng-controller="View2Ctrl"> 
            <div class="container"> 
                <div >
                    <table class="table-condensed">
                        <thead>
                            <tr>
                                <th>id</th>
                                <th>level</th>
                                <th>field</th>
                                <th>country</th>
                                <th>language</th>
                                <th>university</th>
                                <th>link</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="onescholarship in allscholarship">
                                <td>{{ onescholarship.id}}</td>
                                <td>{{ onescholarship.level}}</td>
                                <td>{{ onescholarship.field}}</td>
                                <td>{{ onescholarship.country}}</td>
                                <td>{{ onescholarship.language}}</td>
                                <td>{{ onescholarship.university}}</td>
                                <td>{{ onescholarship.link}}</td>
                            </tr>
                        </tbody>
                    </table> 
                </div>
            </div> 
        </div>
        <button ng-click="shows = false">New search</button>  
    </div>
</div>

How fix Error?

1 Answer 1

0

You have set isArray to false for that call in this line

getScholarshipfindByField:{method:'GET',params:{level:'@level',field:'@field',country:'@country',language:'@language'},isArray:false,responseType:'json'}

Just change that to true and it should come through.

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

7 Comments

After changing isArray to true the error remained the same
Okay I looked again, and it still looks like the solution. Are you sure you have a full refresh of your page? Clear your cache.
Error remained the same after full refresh : Error: [$resource:badcfg] Error in resource configuration for action getScholarshipfindByField. Expected response to contain an object but got an array (Request: GET localhost:8080/scholarship/webresources/scholarship.scholarship) errors.angularjs.org/1.4.7/$resource/……%2Flocalhost%3A8080%2Fscholarship%2Fwebresources%2Fscholarship.scholarship
at angular.js:68 at $http.then.value.$resolved (angular-resource.js:594) at processQueue (angular.js:14745) at angular.js:14761 at Scope.$eval (angular.js:15989) at Scope.$digest (angular.js:15800) at Scope.$apply (angular.js:16097) at done (angular.js:10546) at completeRequest (angular.js:10744) at XMLHttpRequest.requestLoaded (angular.js:10685)
Alright I'm going to need a plunkr where the error is happening, if you make that I'll look.
|

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.