Inline dataviewjs querying a list property containing links with .includes()

Let’s say I have a note with the following property:

test:
  - hello
  - world
  - "[[test]]"

I’m trying to use inline dataviewjs to fetch notes that contain the item “[[test]]”. It works with “hello” but for some reason I cannot get it to work with a link ie. “[[test]]” see the example below:

WORKS: Fetching a note using “hello”:

$= dv.pages('"Notes"').where(n => n.test?.includes("hello")).limit(1).file.link

DOES NOT WORK: Fetching a note using “[[test]]”:

$= dv.pages('"Notes"').where(n => n.test?.includes("[[test]]")).limit(1).file.link
$= dv.pages('"Notes"').where(n => n.test?.includes('"[[test]]"')).limit(1).file.link
$= dv.pages('"Notes"').where(n => n.test?.includes('- "[[test]]"')).limit(1).file.link
$= dv.pages('"Notes"').where(n => n.test?.includes("test")).limit(1).file.link

Any idea how to make this work?

I’ve been losing my mind trying to make this work for hours now so I must be doing something wrong and the documentation related to inline dataviewjs is very minimal. Can’t find an example of that either. I tried all sort of things but nothing will work. Anyone?

Thanks!

The issue you’re encountering is that the contains( ) function expects a string, and the “[[test]]” item is treated as an object. To see this, try…

`$= typeof(dv.pages('"Notes"').where(n => n.test.includes("hello")).test[2])`

One way to avoid this is to use the file.frontmatter property which…

Contains the raw values of all frontmatter in form of key | value text values; mainly useful for checking raw frontmatter values or for dynamically listing frontmatter keys.

https://blacksmithgu.github.io/obsidian-dataview/annotation/metadata-pages/

So a new version of your search would look like

`$= dv.pages('"Notes"').where(n => n.file.frontmatter.test?.includes("[[test]]")).limit(1).file.link`

[Edit: code cleanup and clarity]

1 Like

Oh wow thanks so much! I’ve learned so much during this journey about Obsidian’s and Dataview’s APIs. It’s been a fun yet frustrating-at-times adventure.

Here’s the way I had found (lmao):

dv.el('span', `[[${dv.pages('"Notes"').where(n => n.categories?.some(c => c.path === "📚 Categories/Monthly Notes.md")).array().sort((a, b) => b.file.ctime - a.file.ctime).slice(0, 1).map(p => p.file.name)[0]}]]`)

I’m glad someone actually enlightened me about how to properly do it.

I’ve used your code to do what I want and now it looks like:

dv.pages('"Notes"').where(n => n.file.frontmatter.categories?.includes("[[Monthly Notes]]")).sort(p => p.file.ctime, 'desc').limit(1).file.link

which is SO MUCH BETTER!

Thanks again!

1 Like

Also note that you could use aLinkField.equals(anotherLinkField) to actually check for the links being equal.

1 Like