DataviewJS - Help understanding variable access

What I’m trying to do

I followed the video by fromSergio to create a small books library in a folder called “Books” and I have a basic table view in regular dataview which lists them and sorts them appropriately.

The frontmatter contain (among other items), the following:

---
tag: 📚Book
title: "The Garderobe of Death"
author: [Howard of Warwick]
series: 
  name: Chronicles of Brother Hermitage
  number: 2
status: reading
read:
  start: 2023-03-08
  finish: 
rating: 
---

The basic table sorts appropriately using sort series.name, series.number but I would like to try to create a subheader for each series under the author. To that end, I’ve started with the example from the dataviewjs website, and modified it to fit my layout. It will group by author appropriately, and I am able to (thus far) get it to sort by the series number, but before I move to trying to also group by the series name, I’d like to just display them in a table field. While the sort works, every time I try to include either the series name or number in the table itself, it throws an error.

Things I have tried

Here is the code that works:

'''dataviewjs (backticks changed for forum post)
for (let group of dv.pages('"Books"').groupBy(p => p.author)) { 
  dv.header(3, group.key); 
  dv.table(["Name", "Series", "Num", "Rating"], 
    group.rows  
    .sort(k => k.series.number)
    .map(k => [k.file.link, k.series, k.series, k.rating])) }
'''

That yields a table grouped by author, ordered by series.number, and with name: Chronicles of Brother Hermitage number: 1 in each of the fields containing k.series. However while sorting by k.series.number works, if I include k.series.name or k.series.number in the actual map call, it throws an error saying it cannot read name or number.

Adding this loop right after the initial for loop yields what I think should be correct in the debug console:

  for (let row of group.rows) {
    console.log(row.series);
  }

Any idea how I can access those two values in the actual table output?

Most likely the issue is not to access those when they’re present, but rather when they’re not present. Your query doesn’t limit to the presence of these values, so it’s at least theoretically possible for them to be undefined in some of your cases.

To counter this, you could try adding k.series?.name and/or k.series?.number into your map. This will render these values if they exists, and do a better job of not reporting anything if they’re not existing.

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks) is properly shown.

Many thanks! That was certainly the issue, so I totally misunderstood what it was trying to tell me.

And thank you for the bonus tip, that will come in handy if I remember it :slight_smile:

1 Like

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