I am using Ionic, Angular2, and Firebase in my application.
When the user registers I want to send data the my database and populate the user's profile.
Here is what I am doing:
app.module.ts:
import { AngularFireDatabaseModule } from 'angularfire2/database';
...
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
AngularFireModule.initializeApp(FIREBASE_CONFIG),
AngularFireAuthModule,
AngularFireDatabaseModule, //<-- angular firebase database
IonicStorageModule.forRoot()
],
login.ts:
import { AngularFireDatabase } from 'angularfire2/database';
...
export class LoginPage {
user = {} as User;
constructor(
private afAuth: AngularFireAuth,
private storage: Storage,
private database: AngularFireDatabase, //database
public navCtrl: NavController,
public navParams: NavParams) {
}
//this code gets called when the user presses the register button
async register(user: User) {
try {
const result = await this.afAuth.auth.createUserWithEmailAndPassword(user.email, user.password);
this.afAuth.auth.currentUser.updateProfile({
displayName: user.displayName
}).then(function() {
let currentUser = result;
let userData = {
email: currentUser.email,
name: currentUser.displayName,
uid: currentUser.uid,
created: new Date(),
}
this.database.list('users').set(userData.uid, userData); //<-- This is where error occurs.
}, function(error) {
console.log(error)
});
console.log(result);
} catch(e) {
console.log(e);
}
}
}
Everything seems to work great apart from the database stuff. I can register a user and also login but when I try to send userData
to my database I get the error:
ERROR TypeError: Cannot read property 'database' of undefined
Does anyone have any idea? I'm new to Angular2 so it might be a really simple fix!
Thank you.