On my app's AccountSettingsPage
, I am fetching user data from a SQLite DB
and displaying it on an Ionic
page. However, I encountered the following error:
Error:
TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateRenderer] (ng:///AppModule/AccountSettingsPage.ngfactory.js:87:37)
at Object.debugUpdateRenderer [as updateRenderer] (http://192.168.0.4:8100/build/vendor.js:15109:21)
at checkAndUpdateView (http://192.168.0.4:8100/build/vendor.js:14223:14)
at callViewAction...
account-settings.ts
export class AccountSettingsPage {
currentUser: User;
constructor(private navCtrl: NavController, private navParams: NavParams, private userProvider: UserProvider) {
this.getCurrentUserDetails("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5332311330377d303c3e">[email protected]</a>");
}
getCurrentUserDetails(email: string) {
this.userProvider.getUserByEmail(email)
.then((currentUser: User) => {
this.currentUser = currentUser;
console.log("data: " + JSON.stringify(currentUser));
})
.catch(e => console.error(JSON.stringify(e)));
}
}
user.ts (UserProvider)
getUserByEmail(email: string): Promise<User> {
return this.databaseProvider.getDatabase().then(database => {
return database.executeSql(SQL_SELECT_USER_BY_EMAIL, [email])
.then((data) => {
let user: User;
//loop through all the records and populate the user object. Should be only 1
for (let i = 0; i < data.rows.length; i++) {
user = {
id: data.rows.item(i).id,
name: data.rows.item(i).name,
email: data.rows.item(i).email,
password: data.rows.item(i).password,
confirmPassword: data.rows.item(i).password,
phone: data.rows.item(i).phone,
street1: data.rows.item(i).street1,
street2: data.rows.item(i).street2,
city: data.rows.item(i).city,
state: data.rows.item(i).state,
zip: data.rows.item(i).zip,
active: data.rows.item(i).active
};
}
//return the populated user object back
return user;
});
});
}
account-settings.html (Page)
<ion-header>
<ion-navbar>
<ion-title>Account Settings</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-label>Name: {{currentUser.name}}</ion-label>
<ion-label>Email: {{currentUser.email}}</ion-label>
<ion-label>Password: {{"*****"}}</ion-label>
<ion-label>Phone: {{currentUser.name}}</ion-label>
<ion-label>Street 1: {{currentUser.street1}}</ion-label>
<ion-label>Street 2: {{currentUser.street2}}</ion-label>
<ion-label>City: {{currentUser.city}}</ion-label>
<ion-label>State: {{currentUser.state}}</ion-label>
<ion-label>Zip: {{currentUser.zip}}</ion-label>
</ion-list>
<button ion-button (click)="logout()">Logout</button>
</ion-content>