当刷新页面时,似乎与CouchDB的连接正在重置。
我正在使用Couchdb和Pouchdb创建一个简单的待办事项应用程序,如Blog所示。一切似乎都有效,但是当通过单击浏览器刷新按钮刷新HomePage时,我遇到了问题。使用提供商提取数据。
home.ts 从 provider.ts
加载数据ngOnInit(){
this.DbProvider.getTodos().then((data) => {
this.todos = data;
});
}
getTodos() {
if (this.data) {
return Promise.resolve(this.data);
}
return new Promise(resolve => {
this.db.allDocs({
include_docs: true
}).then((result) => {
this.data = [];
let doc = 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);
});
});
}
provider.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import PouchDB from 'pouchdb';
@Injectable()
export class DbProvider {
data: any;
db: any;
remote: any;
constructor() {}
init(details){
this.db = new PouchDB('myDB');
this.remote = details.userDBs.supertest;
let options = {
live: true,
retry: true,
continuous: true
};
this.db.sync(this.remote, options);
console.log(this.db);
}
}
我遇到的错误
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'allDocs' of undefined
TypeError: Cannot read property 'allDocs' of undefined
at provider.ts:61
at new t (polyfills.js:3)
at DbProvider.webpackJsonp.201.DbProvider.getTodos (provider.ts:59)
at HomePage.webpackJsonp.317.HomePage.ngOnInit (home.ts:38)
at checkAndUpdateDirectiveInline (core.js:12095)
at checkAndUpdateNodeInline (core.js:13598)
at checkAndUpdateNode (core.js:13541)
at debugCheckAndUpdateNode (core.js:14413)
at debugCheckDirectivesFn (core.js:14354)
at Object.eval [as updateDirectives] (HomePage_Host.ngfactory.js? [sm]:1)
at provider.ts:61
at new t (polyfills.js:3)
at DbProvider.webpackJsonp.201.DbProvider.getTodos (provider.ts:59)
at HomePage.webpackJsonp.317.HomePage.ngOnInit (home.ts:38)
at checkAndUpdateDirectiveInline (core.js:12095)
at checkAndUpdateNodeInline (core.js:13598)
at checkAndUpdateNode (core.js:13541)
at debugCheckAndUpdateNode (core.js:14413)
at debugCheckDirectivesFn (core.js:14354)
at Object.eval [as updateDirectives] (HomePage_Host.ngfactory.js? [sm]:1)
at c (polyfills.js:3)
at c (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (core.js:4620)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)
at o (polyfills.js:3)
at <anonymous>