obsidian-dataview
obsidian-dataview copied to clipboard
dv.current() generates an error when file is renamed
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
// 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 note
if (input && dv) {
const mtime = dv.current().file.mtime
const author = dv.current().file.frontmatter.author
const footer = dv.el("div", "", { cls: "note-footer", attr: { id: "footer-container" } });
// format mtime to HH:MM - MM DD, YYYY
const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' };
const date_time = new Date(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 metadata
if (input && dv) {
const active_note = app.workspace.getActiveFile();
const file_cache = app.metadataCache.getFileCache(active_note);
const mtime = active_note.stat.mtime
const author = file_cache.frontmatter.author
const footer = dv.el("div", "", { cls: "note-footer", attr: { id: "footer-container" } });
// format mtime to HH:MM - MM DD, YYYY
const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' };
const date_time = new Date(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
I'm not sure it's as easy as just using getting the active file since there are some cases with functions allowing to change what is to be considered the current file context.
I'm kind of wondering whether this would require to start listening to events related to the renaming of files. That could potentially be a lot of work.
In general I'm not sure when I'll get around to looking deeper into this particular bug, and it kind of feels like a rand case where one easily could either rebuild the current view or reload the note and not experience any consequences from it.
I use dv.current() a lot in my views to pull metadata from the file and display it in some way inside a dataviewjs container. I noticed that refreshing the view often is not enough to properly display the view, but instead the whole note needs to be closed and reopened. This can get a bit annoying especially if you generate the notes through a template containing the views.
I tried replacing dv.current() with app.workspace.getActiveFile() and then load the metadata by app.MetadataCache.getFileCache(). This is often more reliable but can also lead to strange results. In particular if you have several tabbed notes open and close and reopen your vault. Then the views in the non active notes are apparently executed on startup and will all pull their information from the active note and not their own note.
The problem is probably within the obsidian api which apparently doesn't provide a robust way to query the current note. So to fix this it might need the support of the developers behind obsidian.
I noticed that refreshing the view often is not enough to properly display the view, but instead the whole note needs to be closed and reopened.
Just to be clear, have you been able to run the Dataview: Rebuild current view without it updating the view? Or are you talking about some other refreshing?