Forum Discussion

PAULEM's avatar
PAULEM
Advisor
6 years ago

Unsolvable error trying to get tags for a message

I am trying to build a JSON-LD block for our message page and cannot - no matter what I try - get the tags associated with the parent message.  It's driving me nuts!  Here's my code:

 

    <#assign msg_id = env.context.message.uniqueId />

    <#assign messageQuery = "SELECT * FROM messages WHERE id='" + msg_id + "'" />         
    <#assign var_msg = rest("2.0","/search?q=" + messageQuery?url) />

    <#list var_msg.data.items as message>
        <#assign href = message.view_href />
        <#assign subj = message.subject />
        <#assign auth = message.author.login />
        <#assign post = message.post_time?datetime?string />
        <#assign view = message.metrics.views?string />
        <#assign cmnt = message.conversation.messages_count?string />
    </#list>

<#assign usertagQuery = "SELECT * FROM tags WHERE messages.id='" + msg_id + "'" />         
<#assign var_usertags = restadmin("2.0","/search?q=" + usertagQuery?url) />

<#if var_usertags.data.items?has_content>
   <#list var_usertags.data.items as msgtag>
      <#assign utag = msgtag.text?string />
   </#list> 
<#else>
   <#assign utag = "NONE" />
</#if>

I want it to plug into the JSON-LD code as the keywords for the message (as defined by the user):

 

 

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "DiscussionForumPosting",
    "@id": "${href}",
    "headline": "${subj}",
    "author": {
        "@type": "Person",
        "name": "${auth}"
    },
    "dateCreated": "${post}",
    "keywords": "${utag}",
    "award": "${ctag} answer"
}
</script>

All the values above ${utag} fill in correctly - but it ALWAYS falls over here:

 

 

{
    "@context": "http://schema.org",
    "@type": "DiscussionForumPosting",
    "@id": "/t5/TestBoard/Camino-Brooke/gpm-p/1681#M11",
    "headline": "Camino Brooke",
    "author": {
        "@type": "Person",
        "name": "admin"
    },
    "dateCreated": "Jan 30, 2019 4:22:26 PM",
    "keywords": "<!-- FREEMARKER ERROR MESSAGE STARTS HERE --><!-- ]]> --><script language=javascript>//">

I've given up trying to get all the tags - I'll just settle for one at this stage.  (The message I'm testing with has three tags and returns them just fine in the Staging API Browser using:

SELECT * FROM tags WHERE messages.id='1681'

If anyone has a clue as to why this is the case, I'd love to here from you.  I would really appreciate the help.

Thanks, Paul 

 

  • PAULEM 

    It would be helpful if you could post the actual freemarker error that you're getting, there is usually some information that can point in the direction of the problem. So I'm trying to "blindly" debug your issue =D...

    let's see, for example this part of your code:

    <#if var_usertags.data.items?has_content>
       <#list var_usertags.data.items as msgtag>
          <#assign utag = msgtag.text?string />
       </#list> 
    <#else>
       <#assign utag = "NONE" />
    </#if>

    probably won't do what you are intending to, why? Because you're re-assigning the utag variable each time you #list over your tags, therefore even if you had multiple tags, your utag variable will always just contain one, the LAST one! The code can be written a bit more elegantly (and hopefully working as intended) like this:

    <#assign utag = [] />
    <#list var_usertags.data.items>
        <#items as msgtag>
            <#assign utag = utag + [msgtag.text] />
        </#items>
    <#else>
        <#assign utag = ['NONE'] />
    </#list> 

    as you probably understood, the utag variable is now a sequence (think array...), when outputting it in your JS object, you'd simply do:

        "keywords": "${utag?join(' ')?js_string}",

    notice the ?js_string, might be useful in that context, more info here.

    you could also optimize your query a bit by just requesting the necessary information that you need, e.g. only the tag-text:

    <#assign usertagQuery = "SELECT text FROM tags WHERE messages.id = '${msg_id}'" />  

    but that's more a cosmetic thing, the query is correct as far as I can tell.

  • PAULEM 

    It would be helpful if you could post the actual freemarker error that you're getting, there is usually some information that can point in the direction of the problem. So I'm trying to "blindly" debug your issue =D...

    let's see, for example this part of your code:

    <#if var_usertags.data.items?has_content>
       <#list var_usertags.data.items as msgtag>
          <#assign utag = msgtag.text?string />
       </#list> 
    <#else>
       <#assign utag = "NONE" />
    </#if>

    probably won't do what you are intending to, why? Because you're re-assigning the utag variable each time you #list over your tags, therefore even if you had multiple tags, your utag variable will always just contain one, the LAST one! The code can be written a bit more elegantly (and hopefully working as intended) like this:

    <#assign utag = [] />
    <#list var_usertags.data.items>
        <#items as msgtag>
            <#assign utag = utag + [msgtag.text] />
        </#items>
    <#else>
        <#assign utag = ['NONE'] />
    </#list> 

    as you probably understood, the utag variable is now a sequence (think array...), when outputting it in your JS object, you'd simply do:

        "keywords": "${utag?join(' ')?js_string}",

    notice the ?js_string, might be useful in that context, more info here.

    you could also optimize your query a bit by just requesting the necessary information that you need, e.g. only the tag-text:

    <#assign usertagQuery = "SELECT text FROM tags WHERE messages.id = '${msg_id}'" />  

    but that's more a cosmetic thing, the query is correct as far as I can tell.

    • PAULEM's avatar
      PAULEM
      Advisor

      Hi luk 

      Thanks for your advice.  That's what I love about coding - there are so many people willing to help when problems occur. I didn't know how to build an array and now I do!  I'll have to look at some other code I've inherited and see about some refactoring I think. 

      I've attached my code in full (freemarker-script) and the error message (freemarker-error) for your reading pleasure (LOL)!  I realise how hard it is to debug something without running it and appreciate your help.

      Cheers, Paul
      (in sunny Hobart)

      • PAULEM's avatar
        PAULEM
        Advisor

        Hi luk 

        I have (finally) worked out what was wrong.  The <#attempt> wrapper tag -- not visible in my code snippet for you to see -- was only letting the first of the three queries run - the first was successful so it didn't (I'm guessing) see the need to run the second and third queries (?!?!).  When I removed this wrapper tag, the code worked - and worked well thanks to your help above.

        Again, I really appreciate your help.

        Cheers, Paul