0

I am building a simple note-taking app with AngularJS and Firebase. A user creates a profile and then stores his notes on the server.

I have the following data structure in my firebase database:

users
-- user1
---- name : "John Doe",
---- notes
------ note1 : true
notes
-- note1
---- author : "user1",
---- title : "Foo",
---- content : "Bar"

So far I've managed to get all the notes from the currently logged in user by using the following angular factory:

app.factory('noteList', ['fbutil', '$firebaseArray','FBURL', function(fbutil, $firebaseArray, FBURL) {
  var _getByUser = function(uid) {
    var fb = new Firebase(FBURL);
    var norm = new Firebase.util.NormalizedCollection( [fb.child('users/'+uid+'/notes'),'user'], [ fb.child('notes'), 'notes' ]);

    norm = norm.select(
     'notes.title',
     'notes.content',
    );
    return $firebaseArray(norm.ref());
  }

  return {
    getByUser: _getByUser
  }
}]);

However if I try to add a new note with the following function only the note gets stored in the database but nothing happens to users/user1/notes:

$scope.addNote = function(newNote) {
  newNote.author = user.uid;
  if( newNote && !newNote.$id ) {
    console.log('Storing new note in DB');
    var d = new Date();
    newNote.created = d.getTime();
    $scope.notes.$add(newNote).then(function(ref) {
      var id = ref.key();
      console.log('New Note Created with ID:' + id + " for user: " + user.uid);
      var root = new Firebase(FBURL);
      root.child("/users/" + user.uid + "/notes/" + id).set(true);
    });
    $scope.newNote = $scope.blankNote;
  } else {
    var d = new Date();
    newNote.updated = d.getTime();
    $scope.notes.$save(newNote);
  }
};

It seems as if the promise after $scope.notes.$add(newNote) isn't resolved because the console log 'New note Created with ID ...' isn't output.

I've been stuck on this for a couple of hours now and I can't seem to figure out what's wrong. I am fairly new to Angular and Firebase and any help pointing in the right direction will be highly appreciated.

Thanks four your help!

EDIT:

After hours of googling I was finally able to resolve the issue. There was a problem within the Firebase.util Library which prevented the promise from resolving successfully. This pull request from the Firebase.util repo solves the problem and everything is working fine so far.

  • You are saying it dosent update in the html? If so i had the same problem. The reason is its Async : Look here to learn more: http://stackoverflow.com/questions/35531161/taking-long-to-load – amanuel2 Feb 28 '16 at 00:57
  • Not exactly, the promise lies within the promise not being resolved. After hours of googling I was finally able to resolve the issue. There was a problem within the Firebase.util Library which prevented the promise from resolving successfully. [This](https://github.com/firebase/firebase-util/pull/74) pull request from the Firebase.util repo solves the problem and everything is working fine so far. – Michael Ziörjen Feb 29 '16 at 07:58

0 Answers0