2016-10-15 10 views
1

Ionic 2 Rc.0에서 pouchdb를 사용하여 cloudantdb의 데이터를 동기화합니다. 내 app.component.ts 파일Ionic 2 RC.0의 Pouchdb가 작동하지 않습니다.

import { NgModule } from '@angular/core'; 
import { IonicApp, IonicModule } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import {Storage} from '@ionic/storage'; 
import {Data} from '../providers/data'; 

@NgModule({ 
declarations: [ 
MyApp, 
AboutPage, 
ContactPage, 
HomePage, 
TabsPage 
], 
imports: [ 
IonicModule.forRoot(MyApp) 
], 
bootstrap: [IonicApp], 
entryComponents: [ 
MyApp, 
AboutPage, 
ContactPage, 
HomePage, 
TabsPage 
], 
providers: [Storage, Data] 
}) 
export class AppModule {} 

입니다 내 app.module.ts 파일이

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map'; 
import * as PouchDB from 'pouchdb'; 
@Injectable() 
export class Data { 
data: any; 
db: any; 
remote: any; 
username: any; 
password: any; 

constructor(public http: Http) { 
console.log('Hello Data Provider'); 
this.db = new PouchDB('swqms'); 
this.username = 'xxx'; 
this.password = 'xxx'; 
this.remote = 'https://xxx-bluemix.cloudant.com/xxx'; 

let options = { 
    live: true, 
    retry: true, 
    continuous: true, 
    auth: { 
    username: this.username, 
    password: this.password 
    } 
    }; 

this.db.sync(this.remote, options); 

} 

addDocument(doc){ 
this.db.put(doc); 
} 

getDocuments(){ 

console.log("Yes"); 

return new Promise(resolve => { 

    this.db.allDocs({ 

    include_docs: true 

    }).then((result) => { 
    console.log("resutl = " + JSON.stringify(result)) 
    this.data = []; 

    let docs = result.rows.map((row) => { 
     this.data.push(row.doc); 
     resolve(this.data); 
    }); 

    this.db.changes({live: true, since: 'now', include_docs:     true}).on('change', (change) => { 
     this.handleChange(change); 
    }); 

    }).catch((error) => { 

    console.log(error); 

    }); 

    }); 


    } 

    handleChange(change){ 

    let changedDoc = null; 
    let changedIndex = null; 

    this.data.forEach((doc, index) => { 

    if(doc._id === change.id){ 
    changedDoc = doc; 
    changedIndex = index; 
    } 

     }); 

//A document was deleted 
if(change.deleted){ 
    this.data.splice(changedIndex, 1); 
} 
else { 

    //A document was updated 
    if(changedDoc){ 
    this.data[changedIndex] = change.doc; 
    } 
    //A document was added 
    else { 
    this.data.push(change.doc);   
    } 

    } 

    } 

    } 

됩니다

I install pouch db using: npm install pouchdb --save

I install SQLITE Plugin: onic plugin add https://github.com/litehelpers/Cordova-sqlite-storage

I install typings for pouch db: typings install --global --save dt~pouchdb dt~pouchdb-adapter-websql dt~pouchdb-browser dt~pouchdb-core dt~pouchdb-http dt~pouchdb-mapreduce dt~pouchdb-node dt~pouchdb-replication

I also run the following command to generate a Data service: ionic g provider Data

내 data.ts 파일은 다음과 같습니다

import { Component } from '@angular/core'; 
import { Platform } from 'ionic-angular'; 
import { StatusBar } from 'ionic-native'; 
import {Data} from '../providers/data'; 

@Component({ 
template: `<ion-nav [root]="rootPage"></ion-nav>`, 

providers: [Data] 
}) 
export class MyApp { 
rootPage = TabsPage; 

constructor(platform: Platform) { 
platform.ready().then(() => { 
StatusBar.styleDefault(); 
}); 
} 
} 

data.ts 파일에 문제가 있습니다. 이 줄을 주석으로 작성하면 this.db = new PouchDB('sqwms'); 기본 응용 프로그램이 화면에 표시됩니다 (데이터는 obv를 동기화하지 않습니다). 하지만이 줄의 주석을 제거하면 화면에 아무 것도 표시되지 않습니다.

누군가 나를 도와주세요. 감사.

+0

콘솔에 오류가 있습니까? – Ivan

답변

0

콘솔 (브라우저의 경우 ionic serve을 사용하는 경우) 및 설치 과정에서 터미널에 오류가있어 문제에 대한 추가 정보를 제공해야합니다.

대부분의 경우 RC0 이후 코드를 묶는 롤업에 문제가 있습니다. 공식 Ionic 문서의 App Build Scripts을 참조하십시오. 여기서 "타사 모듈 내보내기 오류"절에서는 사용자 정의 롤업 구성 파일에 대해 설명합니다. 그들은 PouchDB를 예로 사용합니다.

0

오류 : pouchdb 6.0.7의

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: 'extend' is not exported by node_modules/js-extend/extend.js (imported by node_modules/pouchdb/lib/index-browser.es.js)

새로운 버전이 오류를 해결하고있다. 내 npm 모듈을 업데이트했는데 이제는 완벽하게 작동합니다.

관련 문제