Hi guys,
I’ve tried to look at a bunch of different topics on templater to piece together a solution but I’m getting stuck trying handle missing values in a JSON file when running a templater script.
What I’m trying to do
Basically, I have created a JSON file with a bunch of data that changes depending on my day and I want to write this data to yesterday’s daily note’s frontmatter when I create today’s daily note. I managed to get that working but whenever there is a day when some fields don’t pull through into the JSON file, then the script bombs out.
Things I have tried
Full disclosure that I definitely at a beginner level with javascript, although I do have some other programming experience but I just don’t understand how null values are being handled.
I tried using the nullish assignment so something like:
const HR = data.find(item => item.name = "heart_rate").data[0].qty ??= -1;
But this doesn’t seem to accurately work. I have also tried splitting it out after the find function like:
const HR = data.find(item => item.name = "heart_rate") ??= -1;
if(!(HR === -1)){
HR = HR.data[0].qty;
}
and finally something like:
data.find(item => item.name = "heart_rate") ??= -1;
if(!(data.find(item => item.name = "heart_rate") === -1)){
HR = data.find(item => item.name = "heart_rate").data[0].qty;
}
else{
HR = -1
}
but none of these work. I keep getting the following error:
“Template syntax error: Invalid left-hand in assignment”
I have no errors if all the data is there which is why I assume the error arises from the missing data handling rather than any other part of the code. I have included the way I write the data to the old notes front matter if anyone wants to see if the error appears there.
tp.hooks.on_all_templates_executed(async () => {
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(prevFile, (frontmatter) => {
frontmatter["steps"] = steps.toFixed(0);
frontmatter["calories burnt"] = calories_burnt.toFixed(0);
frontmatter["heart rate"] = heart_rate.toFixed(2);
frontmatter["sleep hours"] = sleep_hours;
frontmatter["in bed"] = in_bed;
frontmatter["date"] = currDate;
delete frontmatter["ignored"];
});
});
I feel like there has got to be a simpler way to do all of this without so many different workarounds, but since I am still newish to javascript, I really can’t think of a better way