typescript - Angular 和 firebase [push 不是一個函數]
我在使用 Angular 和 Firebase 時遇到錯誤:
錯誤 TypeError: _this.accounts.push 不是一個函數 在 auth.service.ts:25 在 e.g (auth.esm.js:17) 在 Fb (auth.esm.js:20) 在 Bb (auth.esm.js:20) 在 A.push../node_modules/@firebase/auth/dist/auth.esm.js.g.Xb (auth.esm.js:19) 在 kb (auth.esm.js:13) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388) 在 Object.onInvoke (core.js:3824) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387) 在 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();
}
}
提前致謝。傑瑞米