Solved

firebase storage angularfire and angular 2: problem with scope?

Posted on 2016-11-14
1
167 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
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…

809 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