Forum Discussion

cike's avatar
cike
Champion
11 years ago

NonStringException while making a REST API call

Hi,

 

within a custom component I make a REST API call to get all recent threads of my community. From this result I read the author informationen for every thread and make a second REST API call to get ranking information about the author:

<#assign author = rest(thread.messages.topic.author.@href + "/ranking") />

 Up to this point, everything works fine.

 

Now, I want to take my author object and make another REST API call to read the URL for the <right_image>-Tag:

 <#if author.ranking.display.right_image?has_content>
<#assign imageUrl = author.ranking.display.right_image.url />
console.log("${imageUrl}");
</#if>

 After this call I get a Freemarker NonStringException:

NonStringException:For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel):

 

Could someone explain why this exception occurs?

 

Thanks for your help and best regards.

Christian

  • That's the sort of error message you get when you try to reference a property of an object that doesn't exist, or isn't directly convertible to a string (e.g. another object).

     

    My guess is that URL property of author.ranking.display.right_image is not set. You can test this with code like this:

     <#if author.ranking.display.right_image?? && author.ranking.display.right_image.url??>
       <#assign imageUrl = author.ranking.display.right_image.url />
       console.log("${imageUrl}");
     </#if>

    I would also recommend putting looking at the data you're working with by putting the appropriate REST API url in your browser.

  • That's the sort of error message you get when you try to reference a property of an object that doesn't exist, or isn't directly convertible to a string (e.g. another object).

     

    My guess is that URL property of author.ranking.display.right_image is not set. You can test this with code like this:

     <#if author.ranking.display.right_image?? && author.ranking.display.right_image.url??>
       <#assign imageUrl = author.ranking.display.right_image.url />
       console.log("${imageUrl}");
     </#if>

    I would also recommend putting looking at the data you're working with by putting the appropriate REST API url in your browser.

    • cike's avatar
      cike
      Champion

      Hey @nathan,

       

      thanks for reply. That was exactly the hint a looked for.

      Now, I use the ?has_content operator to check the occurence and the content of my nodes and attributes.

       

      Finally, everything works well. :smileyhappy:

       

      Best regards,

      Christian