obsidian-dataview
obsidian-dataview copied to clipboard
[Bug] `this` Bound Incorrectly When Populating Field Using Inline Query
What happened?
When writing a dataview query in file Foo.md
that depends on a field F::
in Bar.md
that is populated by a dataview query, the value of the this
variable is bound incorrectly. Define F
as follows:
F:: `= this.file.name`
When the query runs within the file Bar
, the result is "Bar"
, which is correct. When the query is run as part of evaluating a query in Foo
(in my case a table), this
gets bound to Foo
, and the value in the corresponding column is incorrect.
This can be trivially reproduced as follows:
- Create
Bar.md
as follows:
Baz:: `= this.file.name`
- [[Foo]]
- Preview
Bar
and note that the value of the fieldBaz
is"Bar"
. - Create
Foo.md
as follows:
```dataview
table without id
link(file.name) as Name,
Baz
from [[Foo]]
```
- Preview
Foo
and see that the value in column "Baz" that corresponds to the file name "Bar" is"Foo"
and not"Bar"
.
This may not be a bug. In that case I submit that it's counter-intuitive to bind this
at query execution time, rather than at query definition time. The late binding leads to behaviours like this, where it's difficult to correctly nest queries in an extensible fashion.
DQL
table without id
link(file.name) as Name,
Baz
from [[Foo]]
JS
No response
Dataview Version
0.4.26
Obsidian Version
0.14.2
OS
MacOS
I should note that this is not platform specific. I've reproduced it on iOS too.
Yeah, I don't think this kind of recursion is possible yet. Out of curiosity, can you talk about the use case here a bit?
I often have notes where fields that I may want to query later can be gleaned from the title with a little bit of code (e.g. pulling an author out with a regex). It makes far more sense from a maintenance perspective to have those fields created once (in the note), rather than having to pull them out each time the note is accessed. While Templater is a good fit for when these things are static and unlikely to change, when they are more dynamic it makes far more sense to me to use dataview queries.
I should note that the recursion does work in the sense that the correct expression is evaluated. It's just evaluated in the wrong context.
Can you repro this with DVJS?
To clarify, making the inline query, the main query, or both with DVJS?
The main query. I'm just curious to see what happens. At best, I suspect we'll still see the wrong results.
This does actually work correctly if you write the main query in DVJS. If you write the inline query in DVJS it still fails, much as you would expect.
#1023 also related