Solved

firebase storage angularfire and angular 2: problem with scope?

Posted on 2016-11-14
1
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error received trying to do a system image? 8 46
Rotate images for Web Page 11 28
javascript form submition 2 22
Html date format 9 20
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
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)

730 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