Learning DataviewJS from Dataview

I know my way around Dataview pretty well, but I’d like to convert gradually to DataviewJS because of broader possibilities. But I’m having a hard time understanding the basics.

I’ve got this DV Query (In my Author-note, where I look for book series from said Author):

FROM "4 Reference/Books"
WHERE Author= [[]] AND Type = "Book-Series"

How do I convert this to DVJS? I was guessing with something like this:

dv.list(dv.pages('"4 Reference/Books"')
.where(p =>p.Type == "Book-Series"))
.where(p=> p.Author == dv.current())

But I’m missing something important here…

Thanks for your help!

Hmm… When I get lost in these types of queries, I usually do a console.log(p) or whatever, and look into the console log of the developer tools. And that usually gets me on the right track.

In your case, you might want to take a look at console.log(dv.current()) to see that is a complete object with loads of informations, whilst you might only want the actual file name out of it.

dv.current() is the object, not the current file.link.

Hi @ThurMeijer , you’ve run into one of the things that DQL does for you that is harder to do in JS. (At least for me – I’m not a JS expert.)

The problem is in this line:

.where(p=> p.Author == dv.current())

The key issue here is that:

  • p.Author is expected to be a link object, but could be null or something else.
  • dv.current() is a page object.

A page object and a link object are two different things, and so DataviewJS evaluates the where() clause to false. The DQL engine will dereference the link and compare it to the page’s link, which is what you mean.

To do what you’re trying to accomplish in DataviewJS, you need to compare the path of the link to the path of the current page. Here are some examples:

1 Like

Considering all said by @Craig, I think you need to add an “?” in p.author to avoid “undefined” cases.
Something like:

dv.list(dv.pages('"4 Reference/Books"')
.where(p => p.Type == "Book-Series")
.where(p => p.Author?.path == dv.current().file.path).file.link)

(you need to add at the end what you want from that filtered pages… In the example I added file.link)

1 Like

You’re all lightning fast! Thanks for that!

I’m still struggling, since I don’t know the steps to take to grasp the concepts…

Apparently getting information where ‘Author’ is equal to my current filename is most difficult in this situation. Last line from @mnvwvnm unfortunately doesn’t return anything… I guess I’m missing a “)” somewhere, but where?

you’re pointing to the file name or to the file link?

about the query I wrote above, there’s an extra “)” after the first where… I just edit it.

if any issue with Type replace .where(p => p.Type == "Book-Series") by .where(p => p.Type && p.Type == "Book-Series")

I don’t know if I’m pointing to the file name or the file link. I ‘just’ want to get all Book-Series from the Author on whose page I currently have the query.

Apparently there’s no simple way to convert the DV query 1-on-1 to a DVJS query :sweat_smile:

If Author value is a link, then you are interested to point to the link, not to the name (links and strings are different things).

I think this is the equivalent query:

dv.list(dv.pages('"4 Reference/Books"')
.where(p => p.Type == "Book-Series")
.where(p => p.Author?.path == dv.current().file.path).file.link)

Great, this did it. At least I now this part now.

Now I can experiment from this point forward! Thanks!

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