Dataviewjs - Check if YAML property exists

For a footer script, I’d like to check if a property exists in the current page, and if so, set a variable accordingly. I thought the following would work, but the variable ‘fat’ doesn’t get defined whether or not the property ‘fat’ exists in the note:

if ( dv.current().fat ) { let fat = dv.current().fat + 'g' } else { let fat = '' };

I tried checking if dv.current().fat == null, checking for dv.current().fat.length, no luck. Seems like it should be easy, but my searches of the forum and the Reddit channel have produced nothing. Help!

You need to define it outside of the if statement blocks.

let fat = ""
if (dv.current().fat) 
  fat = dv.current().fat + "g"

I knew it would be something simple. Thanks!

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