Factory returns blank result

Asked

Viewed 140 times

0

Good evening. I have a Factory with the all function that picks up over $http, an external json, only it’s returning blank. The json address is http://alescrideli.com.br/kibelicia/categorias.json

angular.module('kibeliciaApp.services', [])

.factory('Categorias', function($http) {

var categorias = []

return {

  all: function() {
     $http.get('js/categorias.json').then(function (retorno) {
        this.categorias = retorno;
        return this.categorias;
     })
  },

  getCategoria: function(categoriaId) {
     var categoria = {};
     angular.forEach(categorias, function(categoriaCorrente) {
        if (categoriaCorrente.idCategoria === parseInt(categoriaId)) {
           categoria = categoriaCorrente;
        }
     })
     return categoria;
  },

  getProduto: function(produtoId) {
     var produto = {};
     angular.forEach(categorias, function(categoriaCorrente) {
        angular.forEach(categoriaCorrente.produtos, function(produtoCorrente) {
           if (produtoCorrente.idProduto === parseInt(produtoId)) {
              produto = produtoCorrente;
           }
        })
     })
     return produto;
  }

}

});
  • console does not show the error line either?

  • on the console appears Ionic.bundle.js:25642 Syntaxerror: Unexpected token }. does not appear the exact line. It marks line 25642

  • After this.categories = return miss ";" I don’t know if that would be it

4 answers

3


Problem

The error is that you are setting the variable categories as the local variable of the Factory function, but in the successful callback of the $http.get('js/categorias.json') you are assigning this value to the variable this.categorias.

To correct follows below:

all: function() {
 $http.get('js/categorias.json').then(function (retorno) {
    categorias = retorno;
    return categorias;
 })
},

removing the this.

Contextualization of this

Remembering that, in javascript, this is contextualized into each Function. That is, even if you change var categorias []; for this.categorias = [], you have a context problem within the callback Function of $http. If you were to change, you should contextualize the variable this in another variable, for example:

this.categorias = [];
var self = this;


all: function() {
 $http.get('js/categorias.json').then(function (retorno) {
    self.categorias = retorno;
    return self.categorias;
 })
},

0

Passing your Json through Jslint, is giving some errors...

Ta leftover comma in: "textValorOpcao": "",

  • I fixed the json error. Jslint has now approved json. I will reformulate my code and post here

0

Try to use in this Format:

angular.module('MeuModulo')
.factory('MeuService', ['$http' function($http) {
    return {
        getAlgumaCoisa: function() {
            return algumacoisa;
        }
    }
}])
  • @Techies, you can help me with this problem here of $http. I tried all afternoon here to solve this, but I could not. Either returns the result blank or the error

0

I believe this is JSON syntax error, because this error is usually thrown by parsing failure. Checks if the function is actually returning JSON or if it has some syntax error.

  • I rephrased the question. I now posted the code with the local array structure. I want to pull this var categories from an http address, which would be http://alescrideli.com.br/kibelicia/categorias.json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.