2016-09-23 8 views
0

ここに私のコードです。投稿要求がデータを送信せずにサーバーを再起動します(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){} 
} 

問題:

私のフォームを送信するたびに、サーバーは自動的に再起動され、データは記録されず、エラーが表示されるまでに時間がかかりません。

私が得る唯一のエラーは一口から出力された -

エラーTS2339:プロパティ「のsendDataは、」タイプ「HeatmapService」に存在しません。

誰かが助けてくれることを願っています!

答えて

1

フォームを送信すると、onSubmit()関数でthis.heatService.sendData(heatmap)....が呼び出されます。

HeatmapService全体を貼り付けた場合は、sendDataメソッドはありません。それはまさにエラーが言うことです。

この方法を作成するか、質問にコピーして貼り付けてください。

+0

ああ、申し訳ありませんが、私はこの前に 'this.heatService(heatmap)'のようにしようとしていましたが、これまでにいくつかのチュートリアルで作業を見ていました。私はちょうど接続しました。 – since095

1

まあ、エラーメッセージはかなり明白です。 Heatmapクラスを見ると、sendDataメソッドが定義されていないことがわかります。そのため、そのエラーが発生します。

関連する問題