blacksmithgu / obsidian-dataview

A data index and query language over Markdown files, for https://obsidian.md/.
https://blacksmithgu.github.io/obsidian-dataview/
MIT License
6.94k stars 411 forks source link

Bug report Bizarre dataviewjs behavior with async code #1388

Open ghost opened 2 years ago

ghost commented 2 years ago

What happened?

I was trying to get a file's contents within a dataview JS block but noticed some odd behavior. When trying to read the file asynchronously, the DVJS query resolves with the error below. This only happens when using await, but I was able to get around it using Promise.then(). See the JS for the version that works and the one that doesn't. Note that I tried using both dv.io.load() and app.vault.read()

Evaluation Error: SyntaxError: Unexpected end of input
    at DataviewInlineApi.eval (plugin:dataview:19667:21)
    at evalInContext (plugin:dataview:19668:7)
    at asyncEvalInContext (plugin:dataview:19675:16)
    at DataviewJSRenderer.render (plugin:dataview:19699:19)
    at DataviewJSRenderer.onload (plugin:dataview:19283:14)
    at DataviewJSRenderer.e.load (app://obsidian.md/app.js:1:730611)
    at t.e.addChild (app://obsidian.md/app.js:1:731035)
    at t.addChild (app://obsidian.md/app.js:1:1646061)
    at Object.addChild (app://obsidian.md/app.js:1:1645086)
    at DataviewApi.executeJs (plugin:dataview:20208:19)

DQL

No response

JS

const page = dv.pages('"pathToNote"')[0]
// THIS WORKS
dv.io.load(page.file.path).then(content => {
    console.log(content)
        ...
})
// THIS DOES NOT WORK
let content = await dv.io.load(page.file.path)

Dataview Version

0.5.42

Obsidian Version

0.15.9

OS

MacOS

blacksmithgu commented 2 years ago

Both variants seem to work when I replicate this locally - that error appears to be caused by a syntax error. Is there more to your query block?

AB1908 commented 2 years ago

Try the command standalone in the console to check as well.

ghost commented 2 years ago

Unfortunately not. I was getting the error with the exact syntax copy-pasted from above (except for the path). Was totally stumped about it. I'll try again just to be sure. Thank you for your quick response.