Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dv.current() generates an error when file is renamed #2481

Open
fcskit opened this issue Nov 19, 2024 · 0 comments
Open

dv.current() generates an error when file is renamed #2481

fcskit opened this issue Nov 19, 2024 · 0 comments
Labels
bug Something isn't working.

Comments

@fcskit
Copy link

fcskit commented Nov 19, 2024

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

@fcskit fcskit added the bug Something isn't working. label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working.
Projects
None yet
Development

No branches or pull requests

1 participant