I created some javascript functions that read and write to a json file, are suppose to be invoked in angular(from typescript code), using jsonfile library. Here is the code:
function savePatient(patient){
const jsonfile = require('jsonfile')
const file = 'src/resources/patients.json'
jsonfile.writeFile(file, patient, {flag: 'a'}, function(err){
if(err) console.error(err)
})
}
function getPatients(){
const jsonfile = require('jsonfile')
const file = 'src/resources/patients.json'
jsonfile.readFile(file, function(err, obj){
if(err) console.error(err)
console.dir(obj)
return obj
})
}
Here is the declaration of functions in Angular component:
declare function savePatient(patient: Patient);
declare function getPatients(): Patient[];
I managed to successfully call the savePatient()
function, and it does as intended.
When I try to invoke console.log(getPatients())
from inside the Angular component, the output is undefined, but the getPatients()
function itself generates a correct console output from the console.dir(obj)
line.
How am I suppose to get the correct value of the function inside the Angular component?
Also, this project is inside an electron container, if someone may find that relevant.
I found it interesting that the Angular component is the first one to output information to console, even though it would make sense that the js functions should give output before it, considering that the Angular component should be dependent on the return value of the js function, but I don't know what to make of that.