I’m seeking help troubleshooting a DV js query.
I’ve created a DV query to sum a field value. The field is contained in multiple pages.
For context:
There is page for each workout, contained in folder “Workouts/2022”.
There is an inline DV field “WorkoutDuration” on each page.
The code is adequate when I use the slice method to define a subset of the array of pages.
When I try to be a little more elegant and define a subset of the pages using dates (the last 7 days in the example), I get a syntax error I cannot solve (Evaluation Error: SyntaxError: missing ) after argument list at DataviewInlineApi.eval). I suspect it’s related to my very limited understanding of date manipulation in DV and JS.
The entire code is below, but I believe the error is in the short segment bracketed by the comments.
const pagesPre = dv.pages('"Workouts/2022"')
.sort(k => k.file.name, "desc");
// code causing the error is below
let n = 0;
for(let i = 0; i < pagesPre.length; i++) {
if(dv.date(today) - pagesPre[i].file.day <= dur(7 days)) {
let pages[n] = pagesPre[i];
n += 1;
}
}
//end of error-generating code
let sumDuration = 0;
for(let i = 0; i < pages.length; i++) {
if(pages[i].WorkoutDuration) {
sumDuration += pages[i].WorkoutDuration;
}
}
//let sumHours = sumDuration / 60;
let sumHours = Math.floor(sumDuration/60);
let minutes = sumDuration % 60;
dv.paragraph("total workout time: " + sumHours + " hours " + minutes + " minutes " );