Rajeev
Rajeev

Reputation: 46909

Angularjs Type error:Cannot read property 'GET()' of undefined

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

Upvotes: 1

Views: 1166

Answers (3)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

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);
})

Upvotes: 3

lvarayut
lvarayut

Reputation: 15239

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']);

Upvotes: 2

Pyetras
Pyetras

Reputation: 1502

Myops is not being injected. Change it to

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

Upvotes: 1

Related Questions