1

I am struggling a bit understanding how to remove a single item from an object in Firebase with AngularJS.

This is my Firebase (only the interesting bit):

projects
 |
 `--- $id
 |     `-- Name
 |     `-- CreatorID
 |
user_project
 |
 `--- CreatorID
       `-- projectid

Every time a user creates an entry the same is added to the entries with a Creator ID equal to the user ID that I get from Firebase Simple Login. At the same time, my code creates a new item inside user_entries/CreatorID (so that I have a list of entries associated with every user).

Example:

projects
 |
 `--- JlagYdBNX1gyMVCoXBF
 |     `-- Name: "Activity 1"
 |     `-- CreatorID: "simplelogin:29"
 |
user_projects
 |
 `--- simplelogin:29
       `--JleI106xJgZf6_mGHUI: "JlagYdBNX1gyMVCoXBF"

Now my problem is that I can delete an entry, but I don't know how to track back and delete the same from user_entries (so that I am getting many orphans there).

I am using a factory:

app.factory('Project', function ($firebase, FIREBASE_URL) {
  var ref = new Firebase(FIREBASE_URL);
  var projects = $firebase(ref.child('projects')).$asArray();

  var Project = {
    all: projects,
    create: function (project) {
      return projects.$add(project).then(function(projectRef) {
        $firebase(ref.child('user_projects').child(project.creatorUID))
        .$push(projectRef.name());
        return projectRef;
      });
    },
    delete: function (project) {
      var userid = project.creatorUID;
      return projects.$remove(project).then(function(projectRef) {
        //$firebase(ref.child('user_projects').child(userid))
        console.log(project.$id);
      });
    },
  };

  return Project;

});

I know that $remove has a callback returning the $id of the processed item so I tried to change delete to:

delete: function (project) {
  var userid = project.creatorUID;
  return projects.$remove(project).then(function(projectRef) {
    $firebase(ref.child('user_projects').child(userid))
    .$remove(projectRef.$id);
  });
},

but I get

Error: Firebase.child failed: First argument was an invalid path: "undefined". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

Any hint on how to approach this problem?

3
  • I'm assuming you already tried .$remove(project.$id)? Apparently from the docs the object passed to the call back is a regular Firebase object, not an Angular firebase object. I don't know why they decided to do it that way. Commented Apr 3, 2015 at 19:04
  • And of course, following the docs, you should be able to use .$remove(projectRef.key()). Commented Apr 3, 2015 at 19:14
  • Yes I tried with project.$id, no luck. But I guess it's because project.$id is the value, not the key in user_projects. Commented Apr 4, 2015 at 7:02

2 Answers 2

1

The AngularFire documentation indicates that the object passed to the promise callback is a regular Firebase object, not an AngularFire reference. This means that you need to use .key() rather than .$id:

delete: function (project) {
  var userid = project.creatorUID;
  return projects.$remove(project).then(function(projectRef) {
    $firebase(ref.child('user_projects').child(userid))
    .$remove(projectRef.ref());
  });
},
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I understand now... I tried that before and I got this: Error: Firebase.child failed: First argument was an invalid path: "https://my_firebase.firebaseio.com/projects/-Jm24tOoN23k5PFhoNGB". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
0

It may be not the cleanest solution but I found a way to achieve what I needed saving the unique ID of the user_projects entry id into the project itself:

 create: function (project) {
      return projects.$add(project).then(function(projectRef) {
        var projectId = projectRef.name();
        $firebase(ref.child('user_projects').child(project.creatorUID))
        .$push(projectId).then(function(userProjectRef) {
          var userProjectId = userProjectRef.name();
          $firebase(ref.child('projects').child(projectId).child('LinkID')).$set(userProjectId);
        });
        return projectRef;
      });
    },

So that in my firebase I get:

projects
 |
 `--- JlagYdBNX1gyMVCoXBF
 |     `-- Name: "Activity 1"
 |     `-- CreatorID: "simplelogin:29"
 |     `-- LinkID: "JleI106xJgZf6_mGHUI"
 |
user_projects
 |
 `--- simplelogin:29
       `--JleI106xJgZf6_mGHUI: "JlagYdBNX1gyMVCoXBF"

And then it's much easier to delete the entry:

delete: function (project) {
  var userId = project.creatorUID;
  var userProjectId = project.LinkID;
  return projects.$remove(project).then(function(projectRef) {
 $firebase(ref.child('user_projects').child(userId)).$remove(userProjectId);
  });
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.