DataviewJS - How to search for files that contain a particular metadata

Things I have tried

What I’m trying to do

I am trying to search my photos for any photos that contain a particular person. For example, I would like to see a table showing all the photos that have my friend “Julia” in it.

This is how the photo files are set up:


Type: “Photo”
Date: “[[02-06-2022]]”
Tags: test
People: [“Julia”, “Bob”]

Image:: ![[download.jpg]]

This is the code I am using to try and retrieve this photo that contains “Julia”:

let searchterm = dv.current().searchterm; 
let pages = dv.pages(searchterm).where(p => p.Type == "Photo" && p.People.includes("Julia")); 
// Create table 
dv.table(["Photo", "Location", "Date",],pages.map(p => [`<img class="myTableImg" src="${this.app.vault.adapter.basePath}/Assets/${p.Image.path}">`,p.file.link, p.Date])); 

However, I get this error:

Evaluation Error: TypeError: Cannot read properties of null (reading ‘includes’)

I have to use dataviewjs as I can not see the photos if I only use dataview (if there is a way to use dataview and see photos please let me know!)

I am pretty new to JS so a lot of this is going over my head, any help would be appreciated :slight_smile:

Many Thanks.

Summary

This text will be hidden

1 Like

Is the larger bolded text starting “Type” and ending Bob"] your YAML metadata? Could you put the part of your page that includes the YAML and also your “searchterm” field inside a code block the way you did with your dataviewjs code? That will make it easier to figure out what is happening.

A quick/vague guess: dv.pages(searchterm) is returning pages that don’t have a People field. You could add to your where a check that p.People exists to see if you get a different error message. An example re-write of just the part that starts dv.pages, (using two chained where because I have a personal preference for more line-breaks when debugging).

dv.pages(searchterm)
.where(p => p.Type == "Photo" && p.People)
.where(p => p.People.includes("Julia"));
1 Like

Thank you Scholar! That has solved the issue, I was unaware that was how JS worked.

Works perfectly now :slight_smile:

1 Like

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