Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Angular Basics Angular Basic Forms Posting Data Using a Service

Daniel Breen
Daniel Breen
14,943 Points

Comments Getting Added Twice

When I write a comment, it gets added twice.

entry.component.ts

import { Component, Input } from '@angular/core';
import { Entry } from '../shared/entry.model';

@Component({
    selector: 'app-entry',
    templateUrl: 'entry.component.html',
    styleUrls: ['entry.component.css']
})

export class EntryComponent {
    @Input() entry: Entry;

    onCommentAdded(comment: {name: string; comment: string;}) {
        this.entry.comments.push(comment);
    }
}

entry-comment-form.component.ts

import { Component, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { EntryService } from '../shared/entry.service';

@Component({
    selector: 'app-entry-comment-form',
    templateUrl: 'entry-comment-form.component.html' 
})

export class EntryCommentFormComponent {
    name: string;
    comment: string;
    @Input() entryId: number;
    @Output() onCommentAdded = new EventEmitter<{name: string; comment: string;}>();
    @ViewChild('commentForm') commentForm: NgForm;

    constructor(private entryService: EntryService) {

    }

    onSubmit(commentForm: NgForm) {
        let comment = { name: this.name, comment: this.comment};
        this.entryService.addComment(this.entryId, comment)
        .then(() => {
            this.onCommentAdded.emit(comment);
            this.commentForm.resetForm();
        })
        this.onCommentAdded.emit(comment);
        this.commentForm.resetForm();
    }
}

entry.service.ts

import { Entry } from './entry.model';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

@Injectable()
export class EntryService {

    constructor(private http: Http) {

    }

    addComment(entryId: number, comment: {name: string; comment: string;}) {
        return this.http.post(`app/entries/${entryId}/comments`, comment)
            .toPromise();
    }

    getEntries(): Promise<Entry[]> {
        return this.http.get('app/entries')
            .toPromise()
            .then(response => response.json().data as Entry[]);
    }
}

In entry-comment-form.component.ts

        .then(() => {
            this.onCommentAdded.emit(comment);
            this.commentForm.resetForm();
        })
        this.onCommentAdded.emit(comment);
        this.commentForm.resetForm();

You only need those lines one time (inside the 'then' block).

1 Answer

kevin anderson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
kevin anderson
Front End Web Development Techdegree Graduate 41,112 Points

On your entry-form-comment.ts file you have "this.onCommentAdded.emit(comment); this.commentForm.resetForm();" inside the then() and then after it...remove the ones after the then statement....