"Duplicates in a Repeater are not allowed" message on my console

Asked

Viewed 96 times

0

I’m using angular and php. The following message appears on my console:

Error: [ngRepeat:dupes] Duplicates in a Repeater are not allowed. Use 'track by' Expression to specify Unique Keys. Repeater: message in messages, Duplicate key: string:r, Duplicate value: r

I’m not using "track by $index" in my html file because it gives another problem and pretty crazy kkkkk

html:

<div class="card" ng-repeat="mensagem in mensagens track by id">
   <div class="item item-text-wrap">
     <h2>{{mensagem.nome}}</h2>
     <p> {{mensagem.msg}}</p>
     <p>{{mensagem.hora}}</p>
   </div>
</div>

angular:

.controller('logradouroCtrl', function ($scope, $http, $window) {

var pegaMsgsLogra = function () {
    idCep = $window.localStorage.getItem('idCep');
    idUsuario = $window.localStorage.getItem('idUsuario');
    var dados = {
        idUsuario: $window.localStorage.getItem('idUsuario'),
        idCep: $window.localStorage.getItem('idCep')
    }

    $http.post("http://www.vigilantescomunitarios.com/www/php/pegaMsgsLogra.php", dados).success(function (data){
        $scope.mensagens = data;
    });
}

pegaMsgsLogra();

$scope.enviarMsg = function (msg) {
    var enviaMsg = {
        msg: msg,
        idUsuario: $window.localStorage.getItem('idUsuario'),
        idCep: $window.localStorage.getItem('idCep'),
        nome: $window.localStorage.getItem('nome')
    }

    $http.post("http://www.vigilantescomunitarios.com/www/php/enviaMsgLogra.php", enviaMsg).success(function (data){

        console.log(data);
    });
    pegaMsgsLogra();
}

})

php:

while ($linhaMsg=$pegaMsgsLogra->fetch(PDO::FETCH_ASSOC)) {
    @$idAviso = $linhaMsg['idAvisoLogradouro'];
    @$idUsuario = $linhaMsg['idUsuario'];
    @$msg = $linhaMsg['msg'];
    @$foto = utf8_encode($linhaMsg['foto']);
    @$hora = $linhaMsg['hora'];

    $horaP = explode(':', $hora);
    $hora = $horaP[0].':'.$horaP[1];

    //Busca nome do usuário que envio a mensagem
    $pegaUsuarioRemetente=$pdo->prepare("SELECT nome FROM usuarios WHERE idUsuario=:idUsuario");
    $pegaUsuarioRemetente->bindValue("idUsuario", $idUsuario);
    $pegaUsuarioRemetente->execute();

    while ($linhaUsuarioRemetente=$pegaUsuarioRemetente->fetch(PDO::FETCH_ASSOC)) {
            $nomeRemetente  = utf8_encode($linhaUsuarioRemetente['nome']);

    $return = array(
            'id' => $idAviso,
            'nome' => $nomeRemetente,
            'msg' => $msg,
            'foto' => $foto,
            'hora' => $hora
        );

    $mensagens[] = $return;

    }

}

print_r($mensagens);
echo json_encode($mensagens);
  • Try to add track by mensagem.id" in your ng-repeat. Ex: ng-repeat="mensagem in mensagens track by mensagem.id"

  • It didn’t help @Techies.

  • appeared some error? you have an identifier within each object of the list?

  • Just id, I just put, but tb did not help... Hope I will update the codes here in the post

  • Put as is your Json tb and errors

  • Have you tried looking at doc? https://docs.angularjs.org/error/ngRepeat/dupes

  • If track by did not help, your json must be invalid. What is the log of the returned object?

  • Or else try track by $index

  • @Gustavosevero, as Celsomtrindade commented, it may be that json is invalid, of the times I have seen this type of message, very similar to yours.. in my project, it has always been invalid json

Show 4 more comments
No answers

Browser other questions tagged

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