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?
.$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..$remove(projectRef.key()).project.$id, no luck. But I guess it's becauseproject.$idis the value, not the key in user_projects.