2016-10-02 18 views
1

我有兩種類型的文檔的pouchdb:過濾pouchdb文檔 - 連續兩次承諾

  • 待辦事項 - 待辦事項列表
  • 用戶 - 用戶只需把數在pochdb以單獨的形式

當我寫todos時,我也有userNo的變量。這樣我知道他擁有哪些待辦事項。 我有兩個函數供應商獲取待辦事項和用戶號碼。

在HTML列表我想通過管道過濾用戶數待辦事項:

<ion-item-sliding *ngFor="let todo of todos | filter : 'numer_p' : this.todoService.userNo"> 

如果我用手工輸入此號碼它的偉大工程。 Todos按這個數字進行過濾。

的問題是,我在home.ts兩個電話ionViewLoaded:

//call provider to get all docs to show on the list 
this.todoService.getTodos().then((data) => { 
this.todos = data; 
}); 

//call provider to get userNo from pouchdb and set variable in the provider 
this.todoService.getUser().then((result) => { 
    console.log("getBadacz result:" + JSON.stringify(result)); 
    this.todoService.userNo = result['name']; 
}).then(function(second){; 
    console.log("second"); 
}); 

我需要調用getTodos後的getUser。所以我需要使用Promises按順序運行這個函數。 沒有它過濾器中的this.todoService.userNo是未定義的,因爲它尚未設置。它不會工作。

我試圖做這樣的:

this.todoService.getUser().then((result) => { 
    console.log("getBadacz result:" + JSON.stringify(result)); 
    this.todoService.userNo = result['name']; 
}).then(function(second){; 
    console.log("second"); 
    this.todoService.getTodos().then((data) => { 
     this.todos = data; 
    }); 
}); 

但有一個錯誤:

EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'todoService' of null 

我試圖安排本承諾中的序列,但沒有成功。

這裏是小提琴,你可以找到實現的功能提供商: Filter pouchdb docs by userID saved in another doc

非常感謝您的幫助。

+0

是否是angular2?嘗試在你的html模板中從'this.todoService.userNo'中刪除'this.' – yurzui

+0

我認爲這是關於「匆忙」的問題。看來我過分複雜了:)謝謝!有用。 –

回答

0

在這樣的情況下,我會嘗試在待辦事項關鍵字中包含userId,這會提高性能(待辦事項文件可能具有像這樣的密鑰userId_todoId)。

所以你根本不需要兩個承諾。