I have an error with Angular and firebase :
ERROR TypeError: _this.accounts.push is not a function at auth.service.ts:25 at e.g (auth.esm.js:17) at Fb (auth.esm.js:20) at Bb (auth.esm.js:20) at A.push../node_modules/@firebase/auth/dist/auth.esm.js.g.Xb (auth.esm.js:19) at kb (auth.esm.js:13) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388) at Object.onInvoke (core.js:3824) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138)
auth.service.ts :
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { Account } from '../models/Account.model'
import * as firebase from 'firebase';
import DataSnapshot = firebase.database.DataSnapshot;
@Injectable({
providedIn: 'root'
})
export class AuthService {
accounts: Account[] = [];
accountsSubject = new Subject<Account[]>;
constructor() {
this.getAccounts();
}
createNewUser(email: string, password: string, newAccount: Account) {
return new Promise(
(resolve, reject) => {
firebase.auth().createUserWithEmailAndPassword(email, password).then(
() => {
resolve();
this.accounts.push(newAccount); // <-- 25
this.saveAccounts();
this.emitAccounts();
},
(error) => {
reject(error);
}
);
}
);
}
emitAccounts(){
this.accountsSubject.next(this.accounts);
}
saveAccounts(){
firebase.database().ref('/accounts').set(this.accounts);
}
getAccounts(){
firebase.database().ref('/accounts')
.on('value', (data: DataSnapshot) => {
this.accounts = data.val() ? data.val() : [];
this.emitAccounts();
}
);
}
signInUser(email: string, password: string) {
return new Promise(
(resolve, reject) => {
firebase.auth().signInWithEmailAndPassword(email, password).then(
() => {
resolve();
},
(error) => {
reject(error);
}
);
}
);
}
signOutUser() {
firebase.auth().signOut();
}
}
Thank's in avance. Jérémy