DataviewJS query to render number of pages with given tag & key value

Dear fellows, I tried a DataviewJS query to render the number of pages with a certain tag & a key value that is often a link. The prototype query worked for a moment, rendering the expected value, but it stopped working once I opened Obsidian again.

Things I have tried

The sample query in question is:

`$= dv.pages("#source/poem").where(p => p.author.value == "[[Edgar Allan Poe]]").length`

What I’m trying to do

I expect it to return the number of poem notes where the author (author::) is [[Edgar Allan Poe]].

Would you happen to know why this doesn’t work immediately? & would you propose a better way to execute this inline query?

Many thanks in advance!

Well, if not always a link, I think it requires a more complex query.

And about links, why you use p.author.value? I’m not versed in dvjs, but I think you need to target the “path”, not the “value”. And as a path, if the link exists then you need to use includes(), because path is something like “folder/subfolder/my note.md”.

So, try something like

`$= dv.pages("#source/poem").where(p => p.author?.path?.includes("Edgar Allan Po")).length`

p.author?.path?.includes() - I think you need to use both “?”, if not the query fails

That worked, thank you for taking the time.

Turns out it only matters when I use a link in the author field. Would the value property be suitable then? Asking because I don’t understand whether the path property is the most suitable for the case or just a workaround. (From what I understood from the documentation, the path would return the folder location of a file. I’m wondering if I can just get the text value of the author field, regardless if it’s a link or a plain string.)

Regarding the includes() function and the ? marks, could I bother you to tell me from which resource you learned about them? I can’t seem to find anything on the Dataview documentation page, so I’m also wondering if it’s a JavaScript convention or something I totally missed.

Optional Chaining:

Introduced only recently and it made life in Javascript so much easier.

2 Likes

I’m not versed in js (or any code language)… so, take my observations as “relative”.

I think the point is related with how metadata is stored. A string is a string (without any special attribute)… but if a link I think they’re some attributes to consider: I found five attributes related with the link - display, embed, path, subpath and type.

image

As you can see, the only way to target a specific link is via path. (@AlanG corrects me if this is a stupid statement)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.