Search code examples
angularjsangular-resource

Angular $resource new instance is undefined


I've created a factory to define a resource named theorder.

When I call a new instance of theorder (in orderFunctions.js) I get the error: TypeError: undefined is not a function

I've been going around in circles trying to figure out what I'm missing.

order.js

angular.module('app').factory('theorder', function($resource) {

    var OrderResource = $resource('/api/users/:id', {_id: "@id"}, {

        update: {method:'PUT', isArray:false}

    });

   return OrderResource;

});

orderFunctions.js

angular.module('app').factory('orderFunctions', function($http, theorder) {

return {

    createOrder: function(newData) {

        console.log("this is from orderFunctions " + JSON.stringify(newData, null, 4));

        var theorder = new theorder(newData);

      }
  }

});

orderCtrl.js

angular.module('app').controller('requirementsCtrl', function($scope, $http, $location, theorder, orderFunctions, $q) {

$scope.step2 = function() {

var requirements = {
        site: $scope.site,
        speedySpruce: $scope.speedySpruce,
        superSpruce: $scope.superSpruce
    }

    $http.get('/api/theSession').
      success(function(data, status, headers, config) {
        var currentData = data;

        var clone = angular.copy(requirements);

        var newData = angular.extend(clone, currentData);

        orderFunctions.createOrder(newData).then(function() {
                    console.log("success");
            }, function() {
                    console.log("fail");
            })

       }).
      error(function(data, status, headers, config) {

        console.log("another error");

      });

    };

});

Solution

  • The variable containing the new instance of the resource was named the same as the resource. I'm an idiot.

    Replaced

    var theorder = new theorder(newData);
    

    with

    var ARGHHH = new theorder(newData);