Things I have tried
I’ve tried reading up on the dataview docs and various examples from the github page itself. Also tried searching the subreddit and the github issues as well.
What I’m trying to do
I’m trying to query a list of books I read by a specific year using dataviewjs. I am also trying to make the query more readable by using the year property of the date object which are not able to be accessed. The pages that I have use frontmatter to tag the metadata. For example:
type: “book”
started: 2020-09-06
completed: 2021-05-09
Then I have this query
dv.list(dv.pages()
.where(p => p.completed > 2021 && p.type == "book").file.name)
Which works, but if I want to isolate it to a specific year by accessing the year property it does not work like below:
dv.list(dv.pages()
.where(p => p.completed.year == 2021 && p.type == "book").file.name)
Which gives this error:
Evaluation Error: TypeError: Cannot read property 'year' of undefined
at eval (eval at <anonymous> (eval at <anonymous> (app://obsidian.md/app.js:1:1)), <anonymous>:2:25)
at Array.filter (<anonymous>)
at Proxy.where (eval at <anonymous> (app://obsidian.md/app.js:1:1), <anonymous>:9081:39)
at eval (eval at <anonymous> (eval at <anonymous> (app://obsidian.md/app.js:1:1)), <anonymous>:2:2)
at DataviewInlineApi.eval (eval at <anonymous> (app://obsidian.md/app.js:1:1), <anonymous>:12947:16)
at evalInContext (eval at <anonymous> (app://obsidian.md/app.js:1:1), <anonymous>:12948:7)
at eval (eval at <anonymous> (app://obsidian.md/app.js:1:1), <anonymous>:12959:36)
at Generator.next (<anonymous>)
at eval (eval at <anonymous> (app://obsidian.md/app.js:1:1), <anonymous>:42:71)
at new Promise (<anonymous>)
This is strange to me because from the github examples they have this line from a query which seems to access the year property of a returned value from the “time-read” key which seems to be a date. Even if I switch to accessing it by the brackets instead of dot it still doesn’t work, but it seems the dot and bracket access are similar. See the code snippet below:
for (let group of dv.pages("#book").where(p => p["time-read"].year == 2021).groupBy(p => p.genre))
Lastly, I tried a workaround like this also returns nothing:
dv.list(dv.pages()
.where(p => p.completed >= 2021 && p.completed < 2022 && p.type == "book").file.name)
So my main questions are:
- How should I query it by a specific year instead of after a specific time?
- How do I access the year property of a returned value that is presumably date type? Am I not using the correct syntax?
Thank you for the time and reading. I am still new to using this plugin and Obsidian.