I’m new to ionic and am trying to put together a simple app that has a simple data input screen using sqlite. I’m testing on Android, using Chrome to debug. I’ve implemented a database service based on the tutorial here:
https://devdactic.com/ionic-4-sqlite-queries/
However, when I initially navigate to the input screen I get this error:
PatientPage.html:15 ERROR TypeError: Cannot read property 'first_name' of null
at Object.eval [as updateDirectives] (PatientPage.html:15)
Here’s the offending line in my html:
<ion-input name="inp_name" [(ngModel)]="patient.first_name" placeholder="Enter first name"></ion-input>
In my DatabaseService class, I define a Patient object:
export interface Patient {
id: number,
first_name: string,
surname: string,
gender: string,
dob: string
}
Then I initialise a BehaviorSubject like this:
patient = new BehaviorSubject<Patient | undefined>(undefined);
I have a getPatient method:
getPatientById(id): Promise<Patient> {
return this.database.executeSql('SELECT * FROM patient WHERE id = ?', [id]).then(data => {
return {
id: data.rows.item(0).id,
first_name: data.rows.item(0).first_name,
surname: data.rows.item(0).surname,
gender: data.rows.item(0).gender,
dob: data.rows.item(0).dob
}
});
}
And on patient.page.ts I have the following to determine if the page should be in new-patient mode, or edit-patient mode:
ngOnInit() {
this.route.paramMap.subscribe(params => {
let patientId = params.get('id');
// patientId will be 0 for new patients passed from db service
if (patientId != 0) {
console.log('patient.page.ts:ngOnInit:patient found');
this.db.getPatientById(patientId).then(data => {
this.patient = data;
});
}
else {
console.log('patient.page.ts:ngOnInit:patient not found, creating new patient');
this.patient =
{
id: 0,
first_name: '',
surname: '',
gender: '',
dob: ''
}
}
});
}
This creates a new patient if the id was not set when opening the page. Any help much appreciated.