【发布时间】:2017-12-03 22:53:53
【问题描述】:
所以我正在使用 Angular 和 Firestore。出于某种原因,我可以在一个地方订阅可观察到的罚款,但不能在另一个地方订阅,我不知道为什么。我觉得可能是因为我曾经在某个地方订阅过它。我可以没有一个 同时运行订阅和使用 take(1) 还是什么?我没有收到任何错误,它只是停在那里。
private jobDoc: AngularFirestoreDocument<Job>;
job: Observable<Job>;
ngOnInit() {
this.route.params.subscribe(jobId => {
this.createJobPage().then(function(result){
this.job.subscribe(job => {
console.log('this works');
});
}.bind(this));
});
}
// When I load the page we run this function
createJobPage() {
return new Promise(function(resolve){
this.getUser().then(user => {
this.jobDoc = this.afs.doc(`accounts/${user['cid']}/jobs/jobId`);
this.job = this.jobDoc.valueChanges();
this.job.take(1).subscribe(job => {
console.log('this works');
});
resolve();
});
}.bind(this));
}
// Clicking a button saves the info
saveNewJob() {
this.getUser().then(function(user) {
console.log('this works, and shows "this.job" is an observable', this.job);
this.jobDoc.update({id: 1}) // If I add this line it all works
this.job.take(1).subscribe(job => {
console.log('this does not work'); // This doesn't work
});
console.log('This works');
}.bind(this));
}
-------- 更新 --------
我认为这是一个错误。这有效:
this.jobDoc.update({id: 1})
this.job.take(1).subscribe(job => {
console.log('this works');
});
但如果我删除 jobDoc 更新它就不起作用,像这样:
this.job.take(1).subscribe(job => {
console.log('this works');
});
所以看来如果我想订阅这个,我需要先给jobDoc 一点刺激才能唤醒它……有人知道为什么吗?
【问题讨论】:
标签: angular observable angularfire2 google-cloud-firestore