问题
I have this small app with Firebase and i'm implementing CRUD operations. The issue comes when I have to delete a post. I get this error: Error: Reference.child failed: First argument was an invalid path
Here is the service code:
createReview(review) {
return this.db.list('/reviews').push(review)
}
getReviews(){
return this.db.list('/reviews');
}
deleteReview(id:any){
console.log(id)
this.db.object('/reviews/' + id).remove();
}
Component TS
delete(review) {
console.log(review)
this.movieService.deleteReview(review)
}
HTML
<div *ngIf="userReviews">
<ul class="list-group" style="color:black" *ngFor="let r of userReviews">
<li class="list-group-item">
<p class="font-weight-bold text-left">{{r.movie}}</p>
<div class="img-wrapper">
<img class="img-fluid" src="{{ r.poster }}" alt="{{ r.poster }}" />
</div>
<br>
<p class="font-weight-bold text-left"> {{r.review}}</p>
<span class="bottom font-weight-bold text-left">by </span><span class="font-italic">{{r.author}} </span>
<button (click)="delete(r)" class="btn btn-danger">Delete</button>
</li>
</ul>
</div>
I've tried hundreds of diferent aproaches, saw tutorials (got even more confused) and nothing. Can someone explain me how this works? Thank You
@Frank van Puffelen, this is the exported JSON from the firebase collection:
{
"reviews" : {
"-M1R0vU4PksYuf_0TiKc" : {
"author" : "murphy",
"movie" : "Rambo",
"poster" : "https://m.media-amazon.com/images/M/MV5BMTI5Mjg1MzM4NF5BMl5BanBnXkFtZTcwNTAyNzUzMw@@._V1_SX300.jpg",
"review" : "Arrow and bow"
},
"-M1R1xTntiR-qc4A6huw" : {
"author" : "murphy",
"movie" : "Sub Zero",
"poster" : "https://m.media-amazon.com/images/M/MV5BMTI3MzE3MjUzN15BMl5BanBnXkFtZTcwNTcyMjEzMQ@@._V1_SX300.jpg",
"review" : "must be chilli"
}
},
EDIT: the package.JSON @Frank van Puffelen
{
"name": "MoviesDB",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~8.1.0",
"@angular/common": "~8.1.0",
"@angular/compiler": "~8.1.0",
"@angular/core": "~8.1.0",
"@angular/fire": "^5.4.2",
"@angular/forms": "~8.1.0",
"@angular/http": "^7.2.15",
"@angular/platform-browser": "~8.1.0",
"@angular/platform-browser-dynamic": "~8.1.0",
"@angular/router": "~8.1.0",
"@auth0/angular-jwt": "^3.0.1",
"@fortawesome/angular-fontawesome": "^0.5.0",
"@fortawesome/fontawesome-svg-core": "^1.2.27",
"@fortawesome/free-solid-svg-icons": "^5.12.1",
"@ng-bootstrap/ng-bootstrap": "^5.1.4",
"angular-jwt": "^0.1.11",
"angular2-jwt": "^0.2.3",
"bootstrap": "^4.4.1",
"firebase": "^7.9.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.1",
"ng-starrating": "^1.0.11",
"ng2-validation": "^4.2.0",
"ngx-embed-video": "^1.0.4",
"rxjs": "~6.4.0",
"rxjs-compat": "^6.5.3",
"simple-oauth2": "^3.1.0",
"source-map": "^0.7.3",
"tslib": "^1.9.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.801.3",
"@angular/cli": "~8.1.0",
"@angular/compiler-cli": "~8.1.0",
"@angular/language-service": "~8.1.0",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "^5.2.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.4.3"
}
}
回答1:
You seem to be changing the meaning of p
from post
to an id
. Assuming that each post
has an id
property, you probably want:
delete(post) {
this.postService.deletePost(post.id)
}
回答2:
@Frank van Puffelen snapshotChances() was the solution. This methods returns the id from the node to delete in firebase. So, in the service, I get the reviews like this:
getReviews() {
return this.db.list('/reviews').snapshotChanges();
}
And the HTML should be
<button (click)="delete(r.key)" class="btn btn-danger">Delete</button>
And back in the service, the delete method:
deleteReview(id: any) {
console.log(id)
this.db.object('reviews/' + id).remove();
}
来源:https://stackoverflow.com/questions/60466049/error-reference-child-failed-first-argument-was-an-invalid-path