0

I can add and retrieve items just fine but when I delete, it removes the whole node, programs instead.

I tried putting the key directly at the this.programListRef.remove(key manually put here); but still got the same results.

I then checked my programListRef, nothing seemed wrong too.

Here is the code

export class ProgramService { //list variables programListRef: AngularFireList<Program>; programList: Observable<Program[]>; //object variables programObjRef: AngularFireObject<Program>; programObj: Observable<Program>; constructor(private afDB: AngularFireDatabase) { this.programListRef = afDB.list('programs'); //use snapshot changes.map to store key this.programList = this.programListRef.snapshotChanges().pipe( map(changes => changes.map(c => ({ key: c.payload.key, ...c.payload.val() })) ) ); } getProgramList() { return this.programList; } getProgramObj(id:string) { this.programObjRef = this.afDB.object('programs/'+id); this.programObj = this.programObjRef.valueChanges(); return this.programObj; } addProgramObj(programObj:Program) { this.programListRef.push(programObj); } updateProgramObj(id:string, programObj:Program){ this.programListRef.update(id, programObj); } //The problematic code deleteProgramObj(id:string){ this.programListRef.remove(id); } } 

Any hint would be greatly appreciated, thank you for reading!

2 Answers 2

2

Try using update.

deleteProgramObj(id:string){ this.programListRef.update({id: null}); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering! This might be the temporary fix for the "I tried putting the key directly at the this.programListRef.remove(key manually put here); but still got the same results."
1

tl;dr: Key was undefined which made it delete the whole node.

And so it seems that the error was on my side. on my HTML page, I used a ngFor that listed out the objects and I used a different name for the let obj of programList on the click function of delete. Which made it pass Undefined that prompted it to delete the whole node due to remove() method deletes everything if id is not specified.

AngularFire Docs says:

"Deleting the entire list

If you omit the key parameter from .remove() it deletes the entire list.

const itemsRef = db.list('items'); itemsRef.remove();" 

2 Comments

what about your remark this.programListRef.remove(key manually put here)? and still same result ?
That...I fiddled with the code a bit and somehow it worked(changed names of var and methods). Which would mean that this might still be unresolved. I really don't know why it behaved that way

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.