?
Solved

firebase storage angularfire and angular 2: problem with scope?

Posted on 2016-11-14
1
Medium Priority
?
269 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 2000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 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…
Suggested Courses

770 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