Solved

firebase storage angularfire and angular 2: problem with scope?

Posted on 2016-11-14
1
36 Views
Last Modified: 2016-11-14
hi
i am using firebase database to store data and firebase storage to store images. i have a libraries node and an image associated with that library.
i want to delete a library and it's associated image. i manage to do both but not in a logical manner because i get undefined.

this is my code:
  deleteLib(id: string){
    //console.log(id);
    


      let thisLibImagePath = this.libService.getLibImagePlace(id);


      var libsStorageRef = firebase.storage().ref('images/libs/');

      // Create a reference to the image we need to delete
      var LimageRef = libsStorageRef.child(thisLibImagePath);
      

      // Delete the file
      LimageRef.delete().then(function() {
        console.log("deleted successfully");
        this.libraries.remove(id);
        
        
      }).catch(function(error) {
        console.log(error);
      });


      
    //this.libraries.remove(id);
  }

Open in new window


this.libraries.remove(id); this line, which delete the library from the database, doesn't work within the promise, where it should be logical. i get the error that this.libraries is undefined. but if i use the same code at the end, it does recognize this.libraries. so i can delete it at the end, i just don't understand why within the scope of the promise, i can't use it.

what am i missing?

best regards
0
Comment
Question by:derrida
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
this is not the same inside your promise.
Save your ref to your libs to be able to use it.

  deleteLib(id: string){
    //console.log(id);
    


      let thisLibImagePath = this.libService.getLibImagePlace(id);


      var libsStorageRef = firebase.storage().ref('images/libs/');

      // Create a reference to the image we need to delete
      var LimageRef = libsStorageRef.child(thisLibImagePath);
      
      var that = this;
      var libs = this.libraries;

      // Delete the file
      LimageRef.delete().then(function() {
        console.log("deleted successfully");
        libs.remove(id);
        // that.libraries.remove(id); // should work too       
        
      }).catch(function(error) {
        console.log(error);
      });


      
    //this.libraries.remove(id);
  }

Open in new window

1

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to update Firmware and Bios in Dell Equalogic PS6000 Arrays and Hard Disks firmware update.
Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now