johnpapa / angular-tour-of-heroes

Angular - Tour of Heroes - The Next Step after Getting Started
Apache License 2.0
825 stars 1.44k forks source link

Push an array inside a array and after update array and save to database - angular #147

Open natashanodine opened 6 years ago

natashanodine commented 6 years ago

I want to push an array comment in to cabin.comments array and after UPDATE of the cabin array with the new comment in it when the user submits a comment using the onSubmit() and update the cabin with the updateCabin. In the console when running "ng serve --open" I am getting an error:

error TS1128: Declaration or statement expect

I am using a JSON database in the main folder.
I am really new to angular and I am lost.

This is my component file with my onSubmit

  onSubmit(id) {

        this.cabin.comments.push(this.comment);
     this.cabinService.updatePosts(this.cabin.id);

    this.commentForm.reset({
        author: '',
        rating: 5,
        comment: ''
    });
  }

My cabinService

 updatePosts(id){
 const data={"cabin": "Cabin"};
 return this.http.get<{cabin: Cabin}>('http://localhost:3000/cabins/'+id).pipe(
  map(cabin=>{
    console.log(cabin);

     return{
           description: cabin.description,
           featured: cabin.featured,
           comments: cabin.comments

         };

     })
    ).subscribe(updatedEntries=>{
            console.log(updatedEntries);

      updatedEntries.comments.push(data);
      return this.http.put('http://localhost:3000/cabins/' +id, updatedEntries);

    console.log(updatedEntries);

});

My JSON database

      "cabins": [
        {
          "id": 0,
          "description": "Tucked away on the hillside among lush gardens of banana & citrus trees",
          "featured": "true",
          "comments": [
            {
              "rating": 5,
              "comment": "Beautiful place!",
              "author": "John Gomez",
              "date": "2018-10-16T17:57:28.556094Z"
            },
            {
              "rating": 4,
              "comment": "Amazing!",
              "author": "Paul Villar",
              "date": "2017-09-05T17:57:28.556094Z"
            }
          ]
        }
      ]