2017-03-08 47 views
1

我一直在创建一个Angular2应用程序,我想更新Firebase中用户的配置文件,我正在使用AngularFire2。 例如,当我试图更新谁拥有关键的“nmH5ZmawpQgogoCRVFVfNaBN6xg1”,当我点击链接来更新它的错误的用户配置文件出现 EXCEPTION: Error in ./ProfilComponent class ProfilComponent - inline template:82:10 caused by: Firebase.update failed: First argument contains a function in property 'users.nmH5ZmawpQgogoCRVFVfNaBN6xg1.$exists' with contents: function() { return snapshot.exists(); }AngularFire2 Firebase.update failed snapshot.exists()

user.service.ts

users:FirebaseListObservable<any>; 

    updateUser(user:IUser){ 
    this.users=this.af.database.list('/users'); 
    this.users.update(user.uid,user); 
    } 

用户.ts

export interface IUser { 
avatarUrl:string; 
createdDate:string; 
birthDate:string; 
displayName:string; 
email:string; 
gendre:string; 
interests:Interest[]; 
job:Job[]; 
location:ILocation; 
plateform:string; 
uid:string; 
} 

在此先感谢。

+0

解决方法是从用户对象中删除$ key属性和$ exists函数:delete this.user ['$ key'];删除this.user ['$ exists']; – naruto

回答

1

在AngularFire2,列表项和对象有一个$key财产和$exists功能添加。请参阅unwrapMapFn实用程序功能。

从版本2.0.0-beta.8开始,$key$exists属性应该是不可枚举的,并应该被update实现忽略。

如果你不想更新AngularFire2依赖于最新的测试版,您可以使用以下解决方法:

updateUser(user: IUser) { 
    const { $exists, $key, ...userWithout$ } = user as any; 
    this.users = this.af.database.list('/users'); 
    this.users.update(user.uid, userWithout$); 
} 

在最近发布然而,也出现了一些bug修复,所以更新是鼓励。

+0

这是我的界面IUser,导出界面IUser avatarUrl:string; createdDate:string; birthDate:string; displayName:string; email:string; gendre:string; 兴趣爱好[]; 工作:Job []; 位置:ILocation; plateform:string; uid:string;} – naruto

+0

这不会改变答案。您从AngularFire2收到的列表项或对象(毫无疑问,转换为接口)已经具有$ -prefixed属性。你的界面只是一些静态类型;属性将仍然是实例的一部分。 – cartant

+0

谢谢你对我的问题的回答,但我的界面IUser没有属性$ key,$ exists,userWithout $,我已经将这些属性添加到我的界面,但它没有改变 – naruto

0

简而言之:你把更新的第二个参数的对象,可能不包含房产$键和$存在:

const key = user.uid; 
let userWithout$ = user; 
delete userWithout$.$key; 
delete userWithout$.$exists; 

this.users.update(key, userWithout$); 

顺便说一句:额外的问题:

解构行@cartant用途:

const { $exists, $key, ...userWithout$ } = user as any; 

是很不错,但在我的情况没有工作,导致出现以下错误:物业解构模式的预期。

这是ES6的语法,应该工作。 我正在使用typescript 2.4.1。 也许我的tsconfig.json有问题? (我的项目的ts编译目标必须是es5)。