1

This coffee script code is trying to create an angular provider but I get this message: Provider 'ItemsProvider' must define $get factory method.

I have the $get method set. Any idea of what is happening?

'use strict'

    app = angular.module('logica-erp') 

    app.provider 'ItemsProvider', [ ->

        this.$get = ->
                return {

                }

    ]

It fail to load with this message:

Error: [$injector:modulerr] Failed to instantiate module logica-erp due to:
[$injector:pget] Provider 'ItemsProvider' must define $get factory method.

EDIT: This is the javascript generated:

(function() {
  'use strict';
  var app;

  app = angular.module('logica-erp');

  app.provider('ItemsProvider', [
    function() {
      return this.$get = function() {
        return {};
      };
    }
  ]);

}).call(this);
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
victor sosa
  • 899
  • 13
  • 27

1 Answers1

2

CoffeeScript introduces syntax sugar coating that may be poorly understood by both readers and adepts. It is always a good idea to compile it to JS to see what's going on. Implicit returns appear to be the biggest troublemakers in my practice.

In this case CS code is compiled to this

app.provider('ItemsProvider', [
  function() {
    return this.$get = function() {
      return {};
    };
  }
]);

Here provider constructor function returns a value of this.$get (a function) and not this object. Constructor function shouldn't return anything (except the rare case when it should):

app.provider('ItemsProvider', [
  function() {
    this.$get = function() {
      return {};
    };
  }
]);

Beware the arrows.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Thanks, Implicit returns issue – victor sosa Jun 30 '16 at 20:47
  • Can you provide the CoffeeScript code that compiles to the second snippet? – cjbrooks12 Jul 23 '16 at 21:24
  • @cjbrooks12 I don't speak Coffee. Fortunately, there is a [translation service](http://js2.coffee/#try:app.provider%28%27ItemsProvider%27%2C%20[%0A%20%20function%28%29%20{%0A%20%20%20%20this.%24get%20%3D%20function%28%29%20{%0A%20%20%20%20%20%20return%20{}%3B%0A%20%20%20%20}%3B%0A%20%20}%0A]%29%3B) – Estus Flask Jul 23 '16 at 21:43