Hi everyone, just seeking help on this dataview query that I’m running against some of my journal files. This query searches two years back and finds all my journal entries, coercing the date out of the file name and checking against my cutoff.
This is where I’m running into errors, when filtering the individual pages seemingly every file has this console error: TypeError: Cannot read properties of undefined (reading 'name')
However it seems that the filter works. When catching the error and logging the page to the console I can see the file has a name. Any help with this would be wonderful.
// Dataview JS
const heading = "## Open Writing";
const currentFileDate = new Date(dv.current().file.ctime);
const twoYearsAgo = new Date(currentFileDate);
twoYearsAgo.setFullYear(currentFileDate.getFullYear() - 2);
const pages = dv.pages('"Personal/Journal/Days"')
.filter(p => {
try {
// Errors seem to stem from here, cannot read properties of undefined
const titleDateMatch = String(p.file.name).match(/\d{4}-\d{2}-\d{2}/)
// Check if name exists
if (titleDateMatch) {
const titleDate = new Date(titleDateMatch[0]);
return titleDate >= twoYearsAgo;
}
} catch (error) {
console.error(error);
}
return false;
})
.sort((a, b) => {
try {
const dateA = new Date(String(a.file.name).match(/\d{4}-\d{2}-\d{2}/)[0])
const dateB = new Date(String(b.file.name).match(/\d{4}-\d{2}-\d{2}/)[0])
return dateA - dateB; // Sort by inferred date in ascending order
} catch (error) {
console.error(error);
return 0;
}
});
console.log("Filtered and Sorted Pages:", pages);
for (const page of pages) {
try {
const content = await dv.io.load(page.file.path);
const lines = content.split('\n');
let output = [];
let insideHead = false;
dv.header(3, `${page.file.link}`);
for (const line of lines) {
if (line.startsWith(heading)) {
insideHead = true;
} else if (line.startsWith("## ") && insideHead) {
insideHead = false;
break;
} else if (insideHead) {
const match = line.match(/^(#+)\s/);
if (match) {
const newLevel = match[1].length + 1;
output.push(line.replace(/^#+/, '#'.repeat(newLevel)));
} else {
output.push(line);
}
}
}
dv.paragraph(output.join('\n'));
} catch (error) {
console.error("Error processing content for file:", page.file.path, error);
}
}