naruto
naruto

Reputation: 541

AngularFire2 Firebase.update failed snapshot.exists()

I have been creating an Angular2 application, I want to update profiles of users in Firebase, I'm using AngularFire2. For example when I'm trying to update user profile who has a key "nmH5ZmawpQgogoCRVFVfNaBN6xg1",when I click in button to update it an error appears 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(); }

user.service.ts

  users:FirebaseListObservable<any>;

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

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;
}

Thanks in advance.

Upvotes: 1

Views: 217

Answers (2)

Doek
Doek

Reputation: 161

In short: the object you put in the second argument of the update, may not contain the properties $key and $exists:

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

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

By the way: extra question:

the destructuring line @cartant uses:

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

is very nice but didn't work in my case and resulting in the following error: Property destructuring pattern expected.

This is ES6 syntax and should be working. I'm using typescript 2.4.1. Is something wrong with my tsconfig.json perhaps? (ts compile target for my project must be es5).

Upvotes: 0

cartant
cartant

Reputation: 58400

In AngularFire2, list items and objects have a $key property and an $exists function added. See the unwrapMapFn utility function.

As of version 2.0.0-beta.8, the $key and $exists properties should be non-enumerable and should be ignored by the update implementation.

If you don't want to update your AngularFire2 dependency to the latest beta, you can use the following work around:

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

However, there have been several bug fixes in recent releases, so updating is encouraged.

Upvotes: 1

Related Questions