Dataviewjs looking for a page with specific contents, not working as expected

Hi all,

I want to retrieve some pages that contain some metada, but for some reason the where and the include is not working. any insight on what I’m doing wrong will be helpful.

I have the following metadata:

one file:

colors:
 - "[[blue]]"
 - "[[yellow]]"

in different page im trying the following dataviewjs:

dv.table(["Contain Blue: ",""], dv.pages()
	.where(f => f.colors.includes("Blue"))  // I also tied here with f.colors.includes("[[Blue]]")) and to use == instead of includes.
	.sort( s => s.file.link)
	.map((m) => [m.file.link])
)

unfortunately none of them seems to work.
any idea?

Thank you in advance

Where are you Metadata defined? In the frontmatter, or in the body? Have you seen it in the output from:

`= dv.span(dv.current())`

The metadata is at frontmatter:
let me show you the contents of each page:

## page named blue

---
colors:
- "[[blue]]"
- "[[yellow]]"
---
`$=dv.span(dv.current())`

## page named colors

---
# no metadata added
---

dv.table(["Contain Blue: ",""], dv.pages()
  .where(f => f.colors.includes("Blue"))
  .sort( s => s.file.link)
  .map((m) => [m.file.link]) )
dv.span(dv.current())

You’ve got multiple issues in that code segment which needs to be addressed:

  • first and foremost you need to verify that colors exist before trying to read its value. Remember that dv.pages() initially starts out with every page of your vault. So you need to change the where clause to something like f.colors && f.colors.includes("Blue")
  • colors is also an array of links, so it might not work doing a simple includes()
  • it might also not work to check for “Blue” when your value is “blue”

I don’t have time to write it out fully, but hopefully this should get you (or someone else with better time) going to get your issue solved.

1 Like

Thank you, your tips guide me to the solution, I was able to solve it by the following code:

dv.table(["Contain Blue: ",""], dv.pages()
  .where(f => f.colors && f.colors.toString().includes("blue"))
  .sort( s => s.file.link)
  .map((m) => [m.file.link]) )
1 Like

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