在pouchdb回调中调用CommonServiceProvider
时,不会给出联系人详细信息,而是提供错误:
错误错误:未捕获(在承诺中):TypeError:无法设置属性' itemsList'未定义的 TypeError:无法设置属性' itemsList'未定义的
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import PouchDB from 'pouchdb';
import { CommonServiceProvider } from '../../providers/common-service/common-service';
@IonicPage()
@Component({
selector: 'page-contact-lists',
templateUrl: 'contact-lists.html',
})
export class ContactListsPage {
public itemsList;
searchQuery: string = '';
private db;
constructor(private commonServiceProvider: CommonServiceProvider, public navCtrl: NavController, public navParams: NavParams) { }
setupDB() {
this.db = new PouchDB('Contacts');
}
ionViewDidLoad() {
this.setupDB(); //DB Setup
// Function to check if doc is exixts in DB or not
this.db.allDocs({
include_docs: true,
attachments: true
}, function (err, response) {
if (!err) {
//if DB don't have any doc, call the contact service to fatch data
if (response.total_rows == 0) {
this.commonServiceProvider.fetchContacts()
.subscribe((data: any) => {
if (data.responseCode === 0) {
this.itemsList = data.contacts;
// Insert service data in DB
this.db.bulkDocs(data.contacts, (err, result) => {
if (!err) {
}else{
console.log(err);
}
});
} else { }
}, err => {
console.log(err);
});
} else {
this.itemsList = response.rows;
}
} else {
console.log(err);
}
});
}
}