发布请求重新启动服务器而不发送数据(Angular2 / Node.js)

时间:2016-09-23 08:59:49

标签: node.js mongodb angular

这是我的代码;

SERVICE:

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:“HeatmapService”类型中不存在属性“sendData”。

希望有人可以提供帮助!

2 个答案:

答案 0 :(得分:1)

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

如果您已粘贴整个HeatmapService,则没有sendData方法。这正是错误所说的。

创建此方法或将其复制/粘贴到您的问题中。

答案 1 :(得分:1)

错误信息非常明显。 看看你的Heatmap类我可以看到没有定义sendData方法,这就是你得到那个错误的原因。