env.context.message.uniqueId returns null
Hi
I have a script on my ForumTopicPage that captures the id of the original post. I have used this:
<#assign msg_id = env.context.message.uniqueId />
<#assign msgQuery = "SELECT * FROM messages WHERE id='${msg_id}'" />
etc
It works fine - or so I thought. I have found a thread with lots of replies - enough in fact to cause paging to occur - and this code doesn't work on page 2. I get the big yellow error! Here's the advice provided:
InvalidReferenceException:The following has evaluated to null or missing:
==> env.context.message [in template "custom.messageMarkup" at line 14, column 23]
----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related😞
- Failed at: #assign msg_id = env.context.message... [in template "custom.messageMarkup" at line 14, column 5]
----
As suggested, I tried an initial IF test:
<#if env.context.message.uniqueId??>
<#assign msg_id = env.context.message.uniqueId />
etc
</#if>
But this didn't work! I still get the same error.
Does anyone know either
- How to get the ID of the original post when you're not on the first page of the thread, OR
- How to intercept the error and stop it from appearing (ie stop the code from running)?
Thanks in advance
Paul (Tasmania)
There are two ways to get the messages id from context object.
Your condition would be like below code.
<#if env.context.message?? > <#assign msg_id = env.context.message.uniqueId /> <#else> <#assign msg_id = page.context.message.uniqueId /> </#if>
You can get the ID of the currently rendered message
${page.context.message.id}
${env.context.message.id}You can get the unique ID of the currently rendered message
${page.context.message.uniqueId}
${env.context.message.uniqueId}