【问题标题】:Form data not able to fetch angular node mongodb表单数据无法获取角度节点 mongodb
【发布时间】:2018-02-20 18:24:20
【问题描述】:

我有一个应用程序,可以将名字、姓氏和电话号码等联系信息添加到 MongoDB。使用邮递员,它工作得很好。数据添加成功。

但是使用角度应用程序,我有一个接受这三个数据的表单(first_namelast_namephone)。现在,当我提交时,它会显示控制台消息“Failed to add”。

我的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);
    }

  }

当我调试时,我发现 addContact 函数没有从 ng 表单接收数据

收到此错误

请帮我调试代码。 任何帮助将不胜感激。

【问题讨论】:

  • 分享您的服务文件或将其发布到 stackblitz
  • 我可以在控制台看到数据。
  • @RahulSharma 你能看到数据正在通过 contacts.component.ts 的 addContact 函数吗?
  • headers.append('Content-Type', 'application/json');

标签: node.js angular mongodb express


【解决方案1】:

猜一个错字错误,另外一个 angular 2 在 http 拦截器中有缺点。 升级到最新的 Angular。

headers.append('Content-Type', 'application/json');

【讨论】:

  • 您的意思是使用最新的 Angular 可以很容易地调试此类错误?
  • 您可以附加请求的标头,
  • angular 5 对错误处理进行了超级改进,您可以轻松识别确切的错误是什么
  • 将尝试 Angular 5 谢谢
猜你喜欢
  • 1970-01-01
  • 2018-07-07
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2018-03-28
相关资源
最近更新 更多