1

In the following code i get a type error as Type error:Cannot read property 'GET()' of undefined what ami doing wrong here

Services

MyApp.factory('Myops', function($resource) {
    return $resource('/projects/:ID/releases/', {
        ID: "@ID"
    }, {
        "update": {
            method: "PUT"
        },
        "create": {
            method: "POST"
        },
        "get": {
            method: "GET"
        }
    });
});

Controller

MyApp.controller("psCtrls", ['$scope', 'Myops', function($scope, Myops) {
    myops = Myops.GET()
    console.log(myops);

}]);

update 1:

After the inclusion of My ops this is the error

 Error:[$injector:unpr ] Unknown provider :$resource provider<-$resource<-Myops
0

3 Answers 3

3

There is Myops dependency not injected properly

MyApp.controller("psCtrls",['$scope', 'Myops', function($scope,Myops){

}]);

Update 1:

include angular-resource.js

then include the required dependency in your app in order to use $resource service

var app = angular.module('myApp',['ngResouce']);

Update 2

Your resource get call should be .get instead of .GET()

Myops.get().then(function(data){
   console.log(data);
})
Sign up to request clarification or add additional context in comments.

5 Comments

are you sure you inject angular-resource with ngResource depancdancy in you app
After doing the above, Now i run into an error as "Type error" undefined is not a function
did you use .get() instead of .GET()
Yes i used get and it is working now.. But why can i uset GET()
get, update, save, delete & put are predefined method of $resource service, you need to look at docs.angularjs.org/tutorial/step_11
2

You need to include your Myops service as following:

MyApp.controller("psCtrls",['$scope', 'Myops',function($scope,Myops)
{
    myops = Myops.GET()
     console.log(myops);
}]);

Based on your updated question: it seems like you didn't include ngResource in your application, it didn't come with AngularJs. So, you need to manually include it:

<script src="angular-resource.js">

Using:

  • Google CDN e.g. //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-resource.js
  • Bower bower install angular-resource

Then, inject it in your app:

angular.module('app', ['ngResource']);

3 Comments

@Rajeev still not injecting it the right way, check carefully the answers!
Now i run into an error as "Type error" undefined is not a function
@Rajeev, you have to use Myops.get() instead of Myops.GET() as @pankajparkar said.
1

Myops is not being injected. Change it to

...
MyApp.controller("psCtrls",['$scope', 'Myops', function($scope,Myops)
...

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.