Accept date: "" in a DataviewJS query?

Hi everyone! I have this helpful little bit of code that helps me stay organized. It’s in my daily note, and what it does is search for files in a folder that match the date of my daily note.

I often use things like DB Folder or other database tables to add those notes to the calendar in bulk. When I remove them from the schedule using the calendar button, however, it leaves a “” in the metadata of the note (date: “” ) instead of leaving it blank.

This causes the code in my daily note to generate an error.

These “” are annoying AF to clear out and are sometimes hard to find (I have several thousand files).

Is there a line of code I can add to this to just make it accept date: “” as an acceptable piece of metadata, or ignore the “”?

Here’s my code:

const { fieldModifier: f } = this.app.plugins.plugins["metadata-menu"].api;

dv.table(["File Name", "Shared"],
  await Promise.all(
    dv.pages('"Social Media Activities"')
      .where(p => p['date']?.toFormat("yyyy-MM-dd") == dv.current().file.day.toFormat("yyyy-MM-dd"))
      .map(async p => [
        p.file.link,
        await f(dv, p, "shared")
    ])
))

I did try adding an exception (lol), but it didn’t work. I admit I don’t really know what I’m doing:

const { fieldModifier: f } = this.app.plugins.plugins["metadata-menu"].api;

dv.table(["File Name", "Shared"],
  await Promise.all(
    dv.pages('"Social Media Activities"')
      .where(p => p['date']?.toFormat("yyyy-MM-dd") == dv.current().file.day.toFormat("yyyy-MM-dd")!= ("")) 
      .map(async p => [
        p.file.link,
        await f(dv, p, "shared")
    ])
))

The error is:
Evaluation Error: TypeError: p.date?.toFormat is not a function
at eval (eval at (plugin:dataview), :6:30)
at Array.filter ()
at Proxy.where (plugin:dataview:8259:39)
at eval (eval at (plugin:dataview), :6:8) blahblablah…

Thanks so much.

Would it suffice to add another line in front of your date check like:

.where(p => p.date && p.date != "")

Not tested, and I’m not sure how dates would act when compared to a string. And you might even want to check against p.date != '""' possibly.

(Or as a last resort I’d look at the value of p.file.frontmatter.date to check it’s potential value… it would need some null checks and so on as well)


My main point is anyways to do a dual where clause, where the first variant disqualifies any non-existing or non-date variants.

Okay cool thanks so much for responding. I was playing around, trying various things to the where clause before to make it work, but hadn’t quite figured it out. With your help, I finally ended up getting it working:

  .where(p => p['date'] != "" && p['date']?.toFormat("yyyy-MM-dd") == dv.current().file.day.toFormat("yyyy-MM-dd"))

Thanks a lot!

1 Like

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