2016-12-01 53 views
0

我有一個ionic2例如一個問題,我試着打電話給蒙山服務(birthday.service.ts)pouchdb功能誰與PouchDb從我的模塊添加數據(details.ts)離子2無法讀取性能

但我有我這個錯誤:

例外:錯誤./DetailsPage類DetailsPage - 聯模板:22:18造成的:無法讀取未定義

我的服務的特性「郵報」:birthday.service.ts

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等於undefined

@angular/core進口OnInit,並實現它

+0

後,你可以試着改變initDb()ngOnInit()感謝的,我怎麼能我的模塊 – fansz

+0

FE在調用服務在組件的構造函數(不模塊),調用'this.birthdayService.initDB()' – Ivaro18

+0

我嘗試,我有這個錯誤地產「birthdayService」不上鍵入「MyApp的」 – fansz

相關問題