Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
natashanodine opened this issue Oct 24, 2018 · 0 comments

Comments

@natashanodine
Copy link

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"
			}
		  ]
		}
      ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant