2016-10-02 58 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)。

所以你根本不需要两个承诺。