You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I frequently use dataviewjs view containers to render metadata of the current file in my note. An example is provided below, which reads the mtime and author metadata filed from the current file and displays this as a footer to the note.
I used the dataview api (dv.current().file.mtime and dv.current().file.frontmatter.author) to obtain the metadata information. This works fine unless you rename the note and have activated the auto-reload option of the dataview plugin. But if I rename a file the view shows the following error message:
Dataview: Failed to execute view 'assets/javascript/dataview/views/note_footer.js'. TypeError: Cannot read properties of undefined (reading 'file')
This error is apparently caused because Obsidian is not automatically saving the file after a rename.
I found a workaround using Obsidian's own api (app.workspace.getActiveFile()) to retrieve the current note and its metadata.
So I would suggest to implement some type of error handling in dv.current() to catch the TypeError and force Obsidian to rebuild safe the note and rebuild its metadata cache by calling app.workspace.getActiveFile()
DQL
No response
JS
// Version using dv.current() to obtain the metadata, which results in an error if the view// is automatically reloaded after changing the file name of the noteif(input&&dv){constmtime=dv.current().file.mtimeconstauthor=dv.current().file.frontmatter.authorconstfooter=dv.el("div","",{cls: "note-footer",attr: {id: "footer-container"}});// format mtime to HH:MM - MM DD, YYYYconstoptions={year: 'numeric',month: 'long',day: 'numeric',hour: '2-digit',minute: '2-digit'};constdate_time=newDate(mtime).toLocaleDateString('en-US',options);;dv.paragraph(`**last modified:** ${date_time}`,{container: footer});dv.paragraph(`**author:** ${author}`,{container: footer});}// Current work around that solves the problem using obsidians own api to determine// the metadataif(input&&dv){constactive_note=app.workspace.getActiveFile();constfile_cache=app.metadataCache.getFileCache(active_note);constmtime=active_note.stat.mtimeconstauthor=file_cache.frontmatter.authorconstfooter=dv.el("div","",{cls: "note-footer",attr: {id: "footer-container"}});// format mtime to HH:MM - MM DD, YYYYconstoptions={year: 'numeric',month: 'long',day: 'numeric',hour: '2-digit',minute: '2-digit'};constdate_time=newDate(mtime).toLocaleDateString('en-US',options);;dv.paragraph(`**last modified:** ${date_time}`,{container: footer});dv.paragraph(`**author:** ${author}`,{container: footer});}
Dataview Version
0.5.67
Obsidian Version
1.7.6
OS
MacOS
The text was updated successfully, but these errors were encountered:
What happened?
I frequently use dataviewjs view containers to render metadata of the current file in my note. An example is provided below, which reads the mtime and author metadata filed from the current file and displays this as a footer to the note.
I used the dataview api (dv.current().file.mtime and dv.current().file.frontmatter.author) to obtain the metadata information. This works fine unless you rename the note and have activated the auto-reload option of the dataview plugin. But if I rename a file the view shows the following error message:
Dataview: Failed to execute view 'assets/javascript/dataview/views/note_footer.js'. TypeError: Cannot read properties of undefined (reading 'file')
This error is apparently caused because Obsidian is not automatically saving the file after a rename.
I found a workaround using Obsidian's own api (app.workspace.getActiveFile()) to retrieve the current note and its metadata.
So I would suggest to implement some type of error handling in dv.current() to catch the TypeError and force Obsidian to rebuild safe the note and rebuild its metadata cache by calling app.workspace.getActiveFile()
DQL
No response
JS
Dataview Version
0.5.67
Obsidian Version
1.7.6
OS
MacOS
The text was updated successfully, but these errors were encountered: