Post请求不发送数据就重启服务器(Angular2 / Node.js)



我的代码;

服务:

import { Injectable } from "@angular/core";
import { Http, Headers, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Observable";
import 'rxjs/Rx';
import {Heatmap} from "./heatmap"
@Injectable()
export class HeatmapService {
 constructor (private _http: Http) {}
    signup(heatmap: Heatmap) {
        const body = JSON.stringify(heatmap);
        const headers = new Headers({'Content-Type': 'application/json'});
        const options = new RequestOptions({ headers: headers });
        return this._http.post('http://localhost:8080/create', body, options)
            .map(response => response.json())
            .catch(error => Observable.throw(error.json()));
    }
}

组件:

import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, FormControl, Validators } from "@angular/forms";
import { Heatmap } from "./heatmap";
import { HeatmapService } from "./heatmap.service";
@Component({
  templateUrl: 'js/app/heatmap/heatmap.component.html'
})
export class heatmapComponent implements OnInit {
    myForm: FormGroup;
    constructor(private formBuilder: FormBuilder, public heatService: HeatmapService) {}
    onSubmit() {
        const heatmap = new Heatmap(this.myForm.value.name, this.myForm.value.data, this.myForm.value.country);
        console.log(heatmap)
        this.heatService.sendData(heatmap)
            .subscribe(
                data => console.log(data),
                error => console.error(error)
            )
    }
    ngOnInit() {
        this.myForm = this.formBuilder.group({
            name: ['', Validators.required],
            data: ['', Validators.required],
            country: ['', Validators.required]
        });
    }
}
HTML:

<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
    <div class="form-group">
        <label for="name">name</label>
        <input type="text" id="name" class="form-control" formControlName="name">
    </div>
    <div class="form-group">
        <label for="data">data</label>
        <input type="text" id="data" class="form-control" formControlName="data">
    </div>
    <div class="form-group">
        <label for="country">country</label>
        <input type="text" id="country" class="form-control" formControlName="country">
    </div>
    <button type="submit" class="btn btn-primary" [disabled]="!myForm.valid">Sign Up</button>
</form>

的热图:

export class Heatmap {
    constructor(public name: string, public data: string, public country: string){}
}

问题:

每次我提交我的表单,我的服务器自动重启,没有数据记录,不给我时间去看任何错误被控制。

我得到的唯一错误是gulp -

输出

错误TS2339:属性'sendData'不存在类型'HeatmapService'.

希望有人能帮忙!

提交表单时,在onSubmit()函数中调用this.heatService.sendData(heatmap)....

如果您粘贴了整个HeatmapService,则没有sendData方法。这就是错误提示。

创建这个方法或者复制/粘贴到你的问题中

这个错误信息很明显。查看你的Heatmap类,我可以看到没有定义sendData方法,这就是为什么你得到这个错误。

最新更新