Using dataviewjs to find the frontmatter of a linked file

Things I have tried

Does anyone know if there is a dataviewjs equivalent of the following?

`=this.site.conditions.light`

Rendering to:

image

From linked file: GS-43HOLM-BACKYARD-LEFT

Containing:

---
tags: home/43-Holm
aliases: [GS-43HOLM-BACKYARD-LEFT]
season: 2023
description: [in the backyard, directly against the wall on the left]
created: [[2022-09-01]]
modified: <%+ tp.file.last_modified_date() %>
fits:
  tray: [square]
  spaces_available: 4
conditions:
  light: [full sun]
  wind: [sheltered]
  vertical_restriction: [no height restrictions]
  support: [pea sticks, netting, cane]
---

The code is being run in: GB-50-78L-02

Which contains the following YAML:

---
created: [[2022-09-01]]
tags: grow-bag
site: "[[GS-43HOLM-BACKYARD-LEFT]]"
season: 2023
dimensions:
  height: 40
  diameter: 50
pockets:
  amount:
  diameter:
tray: [square]
potting_mix: 
selection: 
manufacturer: "[[Elixir Gardens]]"
purchased: "[[Amazon]]"
---

When I run a regular dataview query, e.g.

```dataview
table final_size.spread AS spacing
from #variety and -"templates"
flatten this.site.conditions.light AS Light
flatten this.site.conditions.wind AS Wind
flatten this.site.conditions.support AS Support
where contains(row.parent.conditions.light, Light) and contains(row.final_size.support, Support)
```

The line this.site.conditions.light is read correctly - meaning that the system navigates from the current note (GB-50-78L-02) to the linked note (GS-43HOLM-BACKYARD-LEFT) and retrieves the subsequent YAML metadata.

Running this:

dv.paragraph(dv.current().file.frontmatter.site)

Returns a clickable link to the linked note:

image

However, the following variants all return an empty string:

```dataviewjs
dv.paragraph(dv.page(dv.current().file.frontmatter.site))
dv.paragraph(await dv.io.load(dv.current().file.frontmatter.site))
```

What Iā€™m trying to do

Extract YAML contents from another file (linked to this one via frontmatter).

While writing this question I came up with a variant that works:

dv.paragraph(dv.page(dv.parse(dv.current().file.frontmatter.site)).file.frontmatter.conditions.light)

If anyone knows a more elegant solution I would be really keen to hear it! :slight_smile:

I like to store stuff in temporary variables, and do you really need to lock it to the frontmatter?

```dataviewjs

const site  = dv.current().site

const conditions = dv.page(site).conditions

dv.paragraph("light conditions: " + conditions.light )
```

The main point is two-folded:

  • If you got a file reference, you can used that in dv.page(), to get access to the page object
  • Given a page object, you can reference the fields directly, without going through file.frontmatter. Rather do dv.current().site or the dv.page(dv.current().site).conditions.light

Does that make sense? If in doubt, do print out stuff to the console using console.log(dv.current()) or similar, and there you can traverse whatever is available for use in your script.

2 Likes

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