Forum Discussion

cgauthier444's avatar
cgauthier444
Contributor
8 years ago

Trying to return post count in custom component

I think this is a pretty basic question (I hope!). 

 

I'm trying to pull back the post count in a custom component that I can then add to a table. Based on what I've seen, the set up might look something like this:

<#assign x= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE board.id = 'installation'"?url) />
<#list x.data.items as message >
<p>$message.subject</p>
</#list>

 

But I get errors when I try to test that. I do know that the SELECT count works (can verify in the API)... but I don't know how to wrap it up for a custom component. 

 

Appreciate any links or suggestions. Cheers

  • Since that query returns a count and not a list of messages, you can't iterate over the results as if it were a list of messages. If you want to iterate over a list messages change your logic to something like this (also note that you need braces after the $ when rendering message.subject):

     

    <#assign x= rest("2.0","/search?q=" + "SELECT id, subject FROM messages WHERE board.id = 'installation'"?url) />
    <#list x.data.items as message >
    <p>${message.subject}</p>
    </#list>

    You unfortunately can't include count(*) with other fields -- it will only return the count if you do, so you will need to make a separate query to get the count of messages.

     

    This won't work (the query will just return the count, not the messages with 'id' and 'subject' field, so the list iteration will fail):

     

    <#assign x= rest("2.0","/search?q=" + "SELECT count(*), id, subject FROM messages WHERE board.id = 'installation'"?url) />
    <#list x.data.items as message >
    <p>${message.subject}</p>
    </#list>

     

    You have to do something like this:

     

    <#assign countX = rest("2.0", "/search?q=" + "SELECT count(*) FROM messages"?url) />
    <div>count: ${countX.data.count}</div>
    
    <#assign x= rest("2.0","/search?q=" + "SELECT id, subject FROM messages"?url) />
    <#list x.data.items as message >
    <p>${message.subject}</p>
    </#list>

     

    -Doug

  • DougS's avatar
    DougS
    Khoros Oracle

    Since that query returns a count and not a list of messages, you can't iterate over the results as if it were a list of messages. If you want to iterate over a list messages change your logic to something like this (also note that you need braces after the $ when rendering message.subject):

     

    <#assign x= rest("2.0","/search?q=" + "SELECT id, subject FROM messages WHERE board.id = 'installation'"?url) />
    <#list x.data.items as message >
    <p>${message.subject}</p>
    </#list>

    You unfortunately can't include count(*) with other fields -- it will only return the count if you do, so you will need to make a separate query to get the count of messages.

     

    This won't work (the query will just return the count, not the messages with 'id' and 'subject' field, so the list iteration will fail):

     

    <#assign x= rest("2.0","/search?q=" + "SELECT count(*), id, subject FROM messages WHERE board.id = 'installation'"?url) />
    <#list x.data.items as message >
    <p>${message.subject}</p>
    </#list>

     

    You have to do something like this:

     

    <#assign countX = rest("2.0", "/search?q=" + "SELECT count(*) FROM messages"?url) />
    <div>count: ${countX.data.count}</div>
    
    <#assign x= rest("2.0","/search?q=" + "SELECT id, subject FROM messages"?url) />
    <#list x.data.items as message >
    <p>${message.subject}</p>
    </#list>

     

    -Doug