2016-12-01 8 views
0

を読んだことができない私はionic2例に問題を持って、私は私のモジュールからPouchDbにデータを追加するサービス(birthday.service.ts)(details.ts)イオン2は、プロパティ

whith pouchdb関数を呼び出そうけど22:18によって引き起こさ:未定義

私のサービスのプロパティ「ポスト」を読み取ることができません:birthday.service.ts - :

例外:インラインテンプレート./DetailsPageクラスDetailsPageでエラーが発生しました私はこのエラーを持っています

import{Injectable} from '@angular/core'; 
    import PouchDB from 'pouchdb' 

    @Injectable() 
    export class BirthdayService { 

    private _db; 
    private _birthdays; 



    initDB(){ 

     this._db = new PouchDB('http://localhost:5984/birthday4'); 

    } 

    add(birthday) { 
     return this._db.post(birthday); 
    } 

私のモジュール:detail.ts

import { Component } from '@angular/core'; 
import { NavParams,ViewController} from 'ionic-angular'; 
import {BirthdayService} from '../../app/services/birthday.service'; 

@Component({ 

    templateUrl: 'details.html', 
    providers:[BirthdayService] 
}) 
export class DetailsPage { 

    public birthday; 
    public isNew = true; 
    public action = 'Add'; 
    public isoDate = ''; 

    constructor (private viewCtrl: ViewController, 
     private navParams: NavParams, 
     public birthdayService : BirthdayService 
    ) { 

    this.birthday = this.navParams.get('birthday'); 
    this.birthday = {Name:" "} 
    this.isNew=true; 

    } 




    save() { 
     this.birthday.Date = new Date(this.isoDate); 

     if (this.isNew) { 
      this.birthdayService.add(this.birthday) 
       .catch(console.error.bind(console)); 
     } else { 
      this.birthdayService.update(this.birthday) 
       .catch(console.error.bind(console)); 
     } 

     this.dismiss(); 
    } 



} 

答えて

0

あなたinitDb()関数が呼び出されていない、thefefore _dbあなたは@angular/coreからOnInitをインポートし、

+0

それを実装した後、ngOnInit()initDb()を変更しようとすることができundefined

に等しく、ありがとう、私のモジュールでサービスを呼び出す方法 – fansz

+0

fe – Ivaro18

+0

私はこのエラーがあります。プロパティ 'birthdayService'がタイプ 'MyApp'に存在しません – fansz