Querying subattributes

Ok so I swear this worked a week ago but I can’t find a SB version to roll back to that fixes the issue, so maybe I’m delusional.

I have the following query

${query[[
  from page = index.tag "page" 
  where page.name:startsWith("Contact/") and page.foreign
  select {
    Name = page.name, 
    Status = page.foreign.status,
    Reported = page.foreign.reported,
  }
  order by name
]]}

This query works.
I can see the foreign.status and foreign.reported subattributes in the table just fine.

However, when I try to query for only those pages that have a subattribute foreign.reported value of false, I get an error:

${query[[
  from page = index.tag "page" 
  where page.name:startsWith("Contact/") and page.foreign.reported == false
  select {
    Name = page.name, 
    Status = page.foreign.status,
    Reported = page.foreign.reported,
  }
  order by name
]]}

returns:
Lua error: Attempting to index a nil value

The frontmatter in question is formatted as such:

    foreign:
      status: true
      reported: false

Any advice?

Thanks!

Posted too soon.
Turns out I added a new contact that did not have the foreign attribute assigned. I needed to additionally filter on any pages that contained that attribute.

This query works fine:

${
  some(
    template.each(
      query[[
        from index.tag "page" 
        where name:startsWith("Contact/") 
        and foreign
        and foreign.reported == false 
        order by name
      ]], 
    templates.fullPageItem)
    ) or "Nothing to report"
  }

I’d like to also note the order of the WHERE clause matters. Having AND foreign after AND foreign.reported == false causes it to fail.

Leaving this up in case anyone else has issues :slight_smile:

1 Like