Angularjs and Firebase - do not show json data in ng-repeat

0

Let's see if someone can help me because I'm stuck at this point. What I'm doing is a test with firebase and its database (Cloud Firestore) + angularjs. This is the following code:

// Initialize Firebase
var config = {
  apiKey: "xxxxx",
  authDomain: "xxxxx.firebaseapp.com",
  databaseURL: "https://xxxxx.firebaseio.com",
  projectId: "xxxxx",
  storageBucket: "xxxxx",
  messagingSenderId: "xxxxx"
};
firebase.initializeApp(config);

// Initialize Cloud Firestore through Firebase
var db = firebase.firestore();
// Add a new document in collection "cities"
var docRef = db.collection("test").doc("prueba1");


var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $http) {
  $scope.firstName = "John";

  //console.log('aaa',docRef.get());
  docRef.get().then(function(doc) {
    if (doc.exists) {
      var docdata = doc.data();
      //console.log(doc.data());
      //console.log("$scope", doc.data().Entrenador);
      //console.log("$scope doc", doc.data());      	    

      $scope.characters = doc.data();
      console.log("$scope chars", $scope.characters );
    } else {
      // doc.data() will be undefined in this case
      console.log("Datos no encontrados!");
    }
  }).catch(function(error) {
    console.log("Error al obtener los datos:", error);
  });

  console.log('Scope!!', $scope);
});
<div ng-controller="myCtrl">
  Name: <input type="text" ng-model="firstName"><br>
  <b>Name</b>: {{ firstName }}<hr>
  
  <div ng-repeat="character in characters">
    {{ character.name}}
  </div>
  
  {{characters.length}}
</div>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase-firestore.js"></script>

When I try to show the data that is in "characters" it does not return anything (it does not give an error either). But the console.log of

  

console.log ("$ scope chars", $ scope.characters);

if it returns information. On the other hand if he returns me correctly John.

Thank you very much for everything:)

    
asked by Ivan 28.06.2018 в 21:55
source

0 answers