Reputation: 65870
user-profile.ts
export interface UserProfile {
id: string;
name: string;
email: string;
phone?: string;
creationTime?: any;
}
service.ts
async signUp(name: string, email: string, password: string): Promise<void> {
try {
const user: firebase.auth.UserCredential = await this.afAuth.auth.createUserWithEmailAndPassword(email, password);
const userProfileDocument: AngularFirestoreDocument<UserProfile> = this.fireStore.doc(`userProfiles/${user.user.uid}`);
const userProfile: UserProfile = {
id: user.user.uid,
email: email,
creationTime: firebase.firestore.Timestamp,
name: name
};
await userProfileDocument.set(userProfile);
} catch (error) {
}
}
One error on console:
[2019-02-28T08:01:58.804Z] @firebase/firestore: Firestore (5.8.3):
The timestampsInSnapshots setting now defaults to true and you no
longer need to explicitly set it. In a future release, the setting
will be removed entirely and so it is recommended that you remove it
from your firestore.settings() call now.
This is the next error:
FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a function (found in field creationTime) at new FirestoreError (http://localhost:8100/vendor.js:77381:28) at ParseContext.push../node_modules/@firebase/firestore/dist/index.cjs.js.ParseContext.createError (http://localhost:8100/vendor.js:96125:16) at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseScalarValue (http://localhost:8100/vendor.js:96467:27) at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseData (http://localhost:8100/vendor.js:96338:29) at http://localhost:8100/vendor.js:96354:41 at forEach (http://localhost:8100/vendor.js:77483:13) at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseObject (http://localhost:8100/vendor.js:96353:13) at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseData (http://localhost:8100/vendor.js:96312:25) at UserDataConverter.push../node_modules/@firebase/firestore/dist/index.cjs.js.UserDataConverter.parseSetData (http://localhost:8100/vendor.js:96177:31) at DocumentReference.push../node_modules/@firebase/firestore/dist/index.cjs.js.DocumentReference.set (http://localhost:8100/vendor.js:97049:45)
Upvotes: 0
Views: 1394
Reputation: 83093
You need to do as follows:
1st error: https://github.com/angular/angularfire2/issues/1993#issuecomment-456481677
2nd Error:
const userProfile: UserProfile = {
id: user.user.uid,
email: email,
creationTime: firebase.firestore.FieldValue.serverTimestamp(),
name: name
};
As explained in the doc, firebase.firestore.FieldValue.serverTimestamp()
will return a "sentinel" object that you use when building the object to be written to Firestore. It will indicate to the server that it should replace it with an actual Timestamp.
Upvotes: 2