Solved

firebase storage angularfire and angular 2: problem with scope?

Posted on 2016-11-14
1
95 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
ID: 41886742
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 66
JS Plugin Chaining 2 32
Auto refresh part of an MVC5 View 7 53
How can I get this SlideToggle to behave? 6 12
Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
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…

948 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

21 Experts available now in Live!

Get 1:1 Help Now