0

I have an application that adds the contact information such as first name, last name and phone number to the MongoDB. With postman, it is working perfectly. Data are being added successfully.

But with angular application, I have a form that accepts those three data(first_name, last_name, phone). Now when I submit it shows console message saying "Failed to add".

My contact.component.html

<form (submit)="addContact()">
    <div class="form-group">
        <label for="first_name">First Name</label>
        <input type="text" [(ngModel)]="first_name" name="first_name" class="form-control">
    </div>
    <!-- /.form-group -->
    <div class="form-group">
        <label for="first_name">Last Name</label>
        <input type="text" [(ngModel)]="last_name" name="last_name" class="form-control">
    </div>
    <!-- /.form-group -->
    <div class="form-group">
        <label for="first_name">Phone</label>
        <input type="text" [(ngModel)]="phone" id="phone" name="phone" class="form-control" required>
    </div>
    <!-- /.form-group -->
    <input type="submit" class="btn-btn-success" value="Add Contact">
</form>

contacts.component.ts

    import { Component, OnInit } from '@angular/core';
  import {ContactService} from '../contact.service';
  import {Contact} from '../contact';


  @Component({
    selector: 'app-contacts',
    templateUrl: './contacts.component.html',
    styleUrls: ['./contacts.component.css'],
    providers:[ContactService]
  })
  export class ContactsComponent implements OnInit {

  contacts: Contact[];
  contact:Contact;
  first_name:string;
  last_name:string;
  phone: string;

    constructor(private contactService: ContactService) {}

    addContact(){

        const newContact = {
            first_name: this.first_name,
            last_name : this.last_name,
            phone: this.phone
        }
        this.contactService.addContact(newContact)
        .subscribe(contact=>{
          console.log(contact); //displaymenssage
          this.contacts.push(contact);
          this.contactService.getContacts() .subscribe( contacts => this.contacts = contacts);
        });

    }

    deleteContact(id:any){
        var contacts = this.contacts;
        this.contactService.deleteContact(id)
        .subscribe(data=>{
            if(data.n==1) {
                for(var i=0; i<contacts.length; i++) {
                    if(contacts[i]._id == id) {
                        contacts.splice(i,1);
                    }
                }
            }
        })
    }
    ngOnInit() {
        this.contactService.getContacts()
        .subscribe( contacts => 
            this.contacts = contacts);
    }

  }

when I debug I see that addContact function is not receiving data from the ng form

getting this error enter image description here

Please help me to debug the code. Any help will be greatly appreciated.

12
  • share your service file or post it in stackblitz Commented Feb 20, 2018 at 9:47
  • stackblitz.com/edit/angular-wvckyu?file=contact.service.ts as discuss here is the file Commented Feb 20, 2018 at 9:51
  • I can see data in the console. Commented Feb 20, 2018 at 9:55
  • @RahulSharma are you able to see that the data is going through the function addContact at contacts.component.ts ? Commented Feb 20, 2018 at 9:57
  • 1
    headers.append('Content-Type', 'application/json'); Commented Feb 20, 2018 at 10:04

1 Answer 1

1

Guess a typo error, and one more angular 2 has disadvantages in http interceptors. Upgrade to latest angular.

headers.append('Content-Type', 'application/json');
Sign up to request clarification or add additional context in comments.

4 Comments

You mean with latest angular it will be easy to debug such errors ?
you can attach headers for request,
angular 5 has super improved error handling, you can easily identify what the exact error is
will try Angular 5 Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.