Syntax error warning on the console running AngularJS

0

I have the following code, which uses factory:

angular.module("fluxo", ["ngRoute"]);

.factory('factCliente', ['$http', function($http) {
var _getData2 = function(id_empresa) {
    return $http.post("php/index.php", id_empresa);
};

return {
    getData2: _getData2
}
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {

//var id_empresa = {id: id_empresa};
var id_empresa = {id: 1};
factCliente.getData2(id_empresa).then(function(response) {
    $scope.mostraTodasContasEntradas = response;
}, function(error) {
    console.log("Ocorreu um erro: " + error);
});
});

And the warning that appears on the console is this:

"SyntaxError: Unexpected token ["

Code php:

<?php
    function mostraContasEntrada($id_empresa){
    header('Content-Type: application/json');
    $pdo = conectar();
    $this->mostraDadosEntrada=$pdo->prepare(
        "SELECT c.categoria, sc.subcategoria, data, valor 
         FROM entrada e 
         JOIN cat_entradas c 
         on c.id_categoria = e.categoria 
         JOIN sub_cat_entrada sc 
         on sc.id_subcategoria 
         WHERE id_empresa=:id_empresa 
         ORDER BY data DESC");
    $this->mostraDadosEntrada->bindValue(":id_empresa", $id_empresa);
    $this->mostraDadosEntrada->execute();

    $return = array();

    while ($r = $this->mostraDadosEntrada->fetch(PDO::FETCH_ASSOC)) {
        $dataP = explode("-", $r['data']);
        $data = $dataP[2].'/'.$dataP[1].'/'.$dataP[0];

        $r['data'] = $data;
        $r['valor'] = number_format($r['valor'],2,',','.');
        $r['subcategoria'] = utf8_encode($r['subcategoria']);
        $return[] = $r;

        //echo $data.'  '.$r['categoria'].'  '.utf8_encode($r['subcategoria']).'  '.number_format($r['valor'],2,',','.')."<br>";

        echo json_encode($return);
    }

    }
<?

Calling the class and function:

require_once "../con/conexao.php";
require_once "../classes/contaEntrada.php";
require_once "../classes/contaSaida.php";

$entrada = new contaEntrada();
$saidas = new contaSaida();

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$id_empresa = $request->id_empresa;

$entrada->mostraContasEntrada($id_empresa);

Does anyone know what problem, what syntax error, is this?

    
asked by anonymous 12.11.2015 / 13:05

2 answers

3

The error occurs because $ http.post returns a promise and not the data itself. To access the return you need to pass two methods to another that is in the promise called then . The first method receives the data itself. The second one receives an error object if this happened.

For example:

Does the Show AllAccounts variable must be being used in part of your code to display a certain list? In your code it is getting a promise and not an array structure. See the corrected example below and note this part: CustomerContent.getData2 (business_id) .then

angular.module("fluxo", ["ngRoute"]);

angular.module("fluxo").config(function ($routeProvider) {
    $routeProvider.when("/entradas", {
        templateUrl: "views/entradas.html",
        controller: "fluxoCtrl"
    })
    .when("/saidas", {
        templateUrl: "views/saidas.html",
        controller: "fluxoCtrl"
    })
    .otherwise({redirectTo: "/index"});
})

.factory('factCliente', ['$http', function($http) {
    var _getData2 = function(id_empresa) {
        return $http.post("php/index.php", id_empresa);
    };

    return {
        getData2: _getData2
    }
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {
    var id_empresa = {id: id_empresa};
    factCliente.getData2(id_empresa).then(function(response) {
        $scope.mostraTodasContasEntradas = response;
    }, function(error) {
        console.log("Ocorreu um erro: " + error);
    });
});
    
12.11.2015 / 19:31
2

I modified the part

return {
    getData2: _getData2,
}

for

return {
    getData2: _getData2
};

I removed the last comma and added a semicolon. In JSlint it passed (adding the angular variable and checking the messy white space option).

It questions the out-of-scope enterprise_id variable and some unused variables.

'use strict';

angular.module("fluxo", ["ngRoute"]);

angular.module("fluxo").config(function ($routeProvider) {

    $routeProvider

    .when("/entradas", {
        templateUrl: "views/entradas.html",
        controller: "fluxoCtrl"
    })

    .when("/saidas", {
        templateUrl: "views/saidas.html",
        controller: "fluxoCtrl"
    })

    .otherwise({redirectTo: "/index"});

})

.factory('factCliente', ['$http', function($http) {
    return {
        getClientes: function(id_empresa) {
            return $http.post("php/index," id_empresa)
        }
    };
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {
    $scope.clientes = [];

    var consultaClientes = function(data, status) {
        $scope.clientes = data;
    };

    factCliente.getClientes().success(consultaClientes);
});
    
12.11.2015 / 13:36