tibewww
tibewww

Reputation: 603

update Current Item Database in Firebase with Angular / Angularfire

I'm using firebase as a DB and Angular to create a backend.

I'm having some issue when I want to update one specific item of my database.

My DB is like this:

 --mydomain 
----posts 
--------POST LIST1 (crazy number and letter are display)
------------author 
------------imageDetails THIS IS WAHT SHOULD BE UPDATED)
------------etc 
--------POST LIST2 (crazy number and letter are display)
------------author 
------------imageDetails 
------------etc 
...
----users 
--------name 
--------email 
--------id

I'd like to be able to be able to update the table imageDetails of the CURRENT Item (posts)

But currently it do this when I try to update:

--mydomain 
----posts 
--------POST LIST1 (crazy number and letter are display)
------------author 
------------imageDetails 
------------etc 
--------POST LIST2 (crazy number and letter are display)
------------author 
------------imageDetails 
------------etc 
--------imageDetails(THIS IS WHERE ITS WRONG)
...
----users 
--------name 
--------email 
--------id

My original code was:

 var firebaseRef = firebase.database().ref().child('posts/');

I also try the following:

 var firebaseRef = firebase.database().ref('posts/' + posts.key +'/test');

without success, I'm having 'posts is not defined'

The full code is like:

    app.controller('myController', ['$scope','$firebaseStorage',function($scope,     $firebaseStorage) {
    var ref = firebase.database().ref().child("posts");
// Create a Firebase Storage reference
  var storage = firebase.storage();
   var firebaseRef = firebase.database().ref('posts/' + posts.key +'/test').push().key;


  var storageRef = storage.ref();
  var filesRef = storageRef.child('files');

  $scope.uploadFile = function(file) {
  console.log("Let's upload a file!");
  console.log($scope.file);
  var storageRef = firebase.storage().ref().child(file.name).put(file);
  storageRef.on('state_changed', function(snapshot) {
            var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;

        }, function() {
            //handle error
        }, function() {
       //url of storage file 
            var downloadURL = storageRef .snapshot.downloadURL;
          firebaseRef.update({'url':downloadURL})
        // firebaseRef.update().ref().child('posts').child({'oui':downloadURL});
          // const dbref = firebase.database().ref().child('posts').child(uid);

     console.log(downloadURL);

        });
    };

 }]);

How can I achieve this? I've been trying to use push, but it doesn't change anything.

Thank you for your help.

Upvotes: 0

Views: 621

Answers (1)

Ankit Kumar Gupta
Ankit Kumar Gupta

Reputation: 1322

app.controller('myController', ['$scope','$firebaseStorage',function($scope,     $firebaseStorage) {
    var ref = firebase.database.ref("posts");
// Create a Firebase Storage reference
  var storage = firebase.storage();



  var storageRef = storage.ref();
  var filesRef = storageRef.child('files');

  $scope.uploadFile = function(file,group) {
  console.log("Let's upload a file!");
  console.log($scope.file);
  var storageRef = firebase.storage().ref().child(file.name).put(file);
  storageRef.on('state_changed', function(snapshot) {
            var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;

        }, function() {
            //handle error
        }, function() {
        var firebaseRef = firebase.database().ref().child('posts');
        var downloadURL = storageRef.snapshot.downloadURL;
            group.imageDetails = downloadURL;
            firebaseRef.child(group.title).update(group);
            console.log(downloadURL);


        });
    };

 }]);

Upvotes: 0

Related Questions