Why does accessing meta data with globals_asset_metadata_<name>:<id> always return a false positive?


#1

I am trying to access the value of a data-asset’s metadate field in SSJS, given its ID:

tileLink = `%globals_asset_metadata_tileLink:${item}%`,

This somewhat works, but it always validates. Eg if I use

${tileLink ? iconHtml : ''}

It will always pass no matter if tileLink is empty or not.

In debugging I printed tileLink.length and saw the it always returned 40.

So, thinking it was an array, I printed typeof tileLink, but it returned string.

Would anyone know what I’m doing wrong here?


(Nick Papadatos) #2

If I understand you correctly - I had a similar issue and ended up doing the following:
I created a const for that value I was trying to get a true/false (string).

 const sponsored =                
 %asset_metadata_related_meta_field^json_decode^as_asset:asset_metadata_<your_value>^empty:false%;

Then inside your forEach loop create another variable
const isSponsored = sponsored[index]; // Match status by index

${isSponsored === "true" ?   `is True then write stuff here` : ``};

Hopefully this relates to what you’re trying to do…


(Iain Simmons) #3

Hi @ADS,

This is due to the order that Matrix will resolve the keywords vs running the SSJS.

You need to force the keyword to only resolve after the SSJS has run, but splitting the string up and having it only become a proper keyword at the end, prior to the final rendering of the page.

e.g.

tileLink = `%` + `globals_asset_metadata_tileLink:${item}%`,

When Matrix comes across the percentage sign %, it looks to see if the very next characters are asset, globals, frontend or a handful of other possible keyword prefixes. Here it would be the closing backtick so it ignores it (i.e. treats it just as regular text).

Then the SSJS runs and combines it into an actual keyword, something like %globals_asset_metadata_tileLink:12345%, after which Matrix does another pass to resolve keywords again, and this time would do the actual replacement to get the value of that metadata field for that asset.

The Server Side JavaScript page describes the processing order of SSJS in more detail. I think you could also try adding evalkeywords="post" to the <script runat="server"> tag to get a similar effect with your original code, but depending on whether you have other global variables in there, it may affect other SSJS code.

The Create a dynamic asset listing using SSJS tutorial also has an example of splitting the keywords.

Hope that helps!
—iain