DataviewJS TypeError: Cannot read properties of undefined (reading 'name')?

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);
    }
}

When inside a DataViewArray and doing sorting, it’s sufficient to showcase which element you use for sorting, so try doing .sort( p => p.file.day.year ) (given that your file name is in ISO 8601 format with YYYY-MM-DD ).

If that doesn’t work, try introducing some other error in your script, to pinpoint on exactly which line you have the error.

1 Like

That was it! It turns out that my solution was failing on the sort call, that’s what I get for having ChatGPT help with it :joy:

const heading = "## Open Writing";
const currentFileDate = new Date(dv.current().file.ctime);
const twoYearsAgo = new Date(currentFileDate);
twoYearsAgo.setFullYear(currentFileDate.getFullYear() - 2);

const pages = await dv.pages('"Personal/Journal/Days"')
    .filter(p => {
        try {
            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(p => p.file.name, 'desc');  // Thankfully ISO Strings sort naturally