Now I’d like to add a dv.paragraph that describes books read this year. The book note frontmatter includes a variable DateFinished: followed by the date so e.g. DateFinished: 2023-01-30
I’d like to have a dv.paragraph that goes something like
let BooksThisYear = dv.pages('"Book Log"').where(p => p.DateFinished.includes(year));
The reason I’d like it to be dynamic, i.e. that it include(year) instead of include("2023") (which also doesn’t work btw) is so that next year I won’t have to go through the code to change everything to 2024. I guess I’m lazy like that haha.
If you don’t want to be bothered going by the format option, you could also do something like:
```dataviewjs
const year = DateTime.now().year
let BooksThisYear = dv.pages('"Book Log"')
.where(p => p.DateFinished.year == year);
```
Bonus tip: How to present code properly in a forum post
If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks) is properly shown.
Thanks, I’ve updated the post. In the preview my code lines were all single lines, so I thought it would look all right, but in the post itself it went into multiple lines.
As for your solutions: Neither work, unfortunately.
For the first thing, I get
Cannot read properties of null (reading 'toFormat')
For the const thing, I get
Cannot read properties of null (reading 'year')
Any ideas?
Btw. will this work with my date formats? I use YYYY-MM-DD
I’m a little tired, and made the same mistaken in both queries. If you do p.DateFinished.something in a where clause when DateFinished is not present, it’ll fail with that message.
You need to do p.DateFinished?.something, or more specific either do the p.DateFinished.toFormat("yyyy") in the first query, or p.DateFinished?.year in the second query. Then both should work.
The extra question mark, ?, allows javascript to bail out gracefully, if the preceding field is not present in any of your files.
As long as the dates are proper dates, and not just strings, it should work, yes. And YYYY-MM-DD should be parsed by dataview(/js) as a proper date (in most cases).
let year = DateTime.now().toFormat("yyyy");
let BooksThisYear = dv.pages('"Book Log"').where(p => p.DateFinished?.toFormat("yyyy") == year);
dv.paragraph("# This year (" + year + ") I've read " + BooksThisYear + " books.")
Yeah, you’re listing the entire page object, not the name, nor the link, nor any of the other stuff available from a page. So if you want a list of the names, instead of BooksThisYear, do BooksThisYear.file.name.join(", "),
and similar use file.link if you want links to the books. (This is within the dv.paragraph() context)
The .join(", ") is kind of superfluous, but if I don’t add it I sometimes to get an extra ] at the end, and I’m not quite sure why. So I tend to add it, to be on the safe side.
He’s asking two questions, and the first one regarding the number of books he got covered, so that was his intention, but he had some issues related to getting the list of books.
Do note, that in your query you’re missing the datefinished?.year, which most likely works for you because every file in your test set up has the datefinished defined, but if you add another note not having that field, you’d get the same error as the OP got.
In this case I was actually just going for the number of books read this year, but thanks to your advice I’ll now add a page that includes the list of books in the current year. Thanks!!