更新 AngularFire2 对象时出错
Error updating an AngularFire2 object
我正在使用 AngularFire2 检索 Firebase 对象:
// AuthService
public getUserID (): Observable<string> {
return this.afAuth.authState.map(user => user ? user.uid : null)
}
// AccountService
private profileInfo: FirebaseObjectObservable<any>
constructor (private db: AngularFireDatabase, private authService: AuthService) {
this.profileInfo = authService.getUserID().flatMap(uid => {
return db.object(`Users/${uid}`)
}) as FirebaseObjectObservable<any>
// have to explicitly cast it or the compiler complains
// not sure if that's related
}
这一切似乎工作正常,我可以从中读取正确的数据,但是当我尝试更新它时:
public changeProfileInfo (name: string) {
this.profileInfo.update({name: name})
}
函数执行时出现运行时错误:TypeError: this.profileInfo.update is not a function
。我在这里做错了什么?
我设法通过在地图中手动分配它来实现它:
this.profileInfo = authService.getUserID().flatMap(uid => {
const profileInfo = db.object(`Users/${uid}`)
this.profileInfo = profileInfo
return profileInfo
}) as FirebaseObjectObservable<any>
我对这个解决方案不满意,感觉真的很乱,但至少它有效。
Update:经过进一步测试,事实证明,如果您注销并重新登录,这会引发错误,因为它失去了对 uid 的引用。我对解决方法的最佳尝试甚至更丑陋:
this.profileInfo = authService.getUserID().flatMap(uid => {
const profileInfo = db.object(`Users/${uid}`)
this.profileInfo.$ref = profileInfo.$ref
this.profileInfo.update = profileInfo.update
return profileInfo
}) as FirebaseObjectObservable<any>
(如果你想使用任何其他方法,你也需要单独添加它们)
我正在使用 AngularFire2 检索 Firebase 对象:
// AuthService
public getUserID (): Observable<string> {
return this.afAuth.authState.map(user => user ? user.uid : null)
}
// AccountService
private profileInfo: FirebaseObjectObservable<any>
constructor (private db: AngularFireDatabase, private authService: AuthService) {
this.profileInfo = authService.getUserID().flatMap(uid => {
return db.object(`Users/${uid}`)
}) as FirebaseObjectObservable<any>
// have to explicitly cast it or the compiler complains
// not sure if that's related
}
这一切似乎工作正常,我可以从中读取正确的数据,但是当我尝试更新它时:
public changeProfileInfo (name: string) {
this.profileInfo.update({name: name})
}
函数执行时出现运行时错误:TypeError: this.profileInfo.update is not a function
。我在这里做错了什么?
我设法通过在地图中手动分配它来实现它:
this.profileInfo = authService.getUserID().flatMap(uid => {
const profileInfo = db.object(`Users/${uid}`)
this.profileInfo = profileInfo
return profileInfo
}) as FirebaseObjectObservable<any>
我对这个解决方案不满意,感觉真的很乱,但至少它有效。
Update:经过进一步测试,事实证明,如果您注销并重新登录,这会引发错误,因为它失去了对 uid 的引用。我对解决方法的最佳尝试甚至更丑陋:
this.profileInfo = authService.getUserID().flatMap(uid => {
const profileInfo = db.object(`Users/${uid}`)
this.profileInfo.$ref = profileInfo.$ref
this.profileInfo.update = profileInfo.update
return profileInfo
}) as FirebaseObjectObservable<any>
(如果你想使用任何其他方法,你也需要单独添加它们)