0

I am attempting to render the variable cat which is a string within an ExpressJS statement AND a script tag.

Here is my code:

<script>
            function detectChange() {
                var place;
                cat = document.getElementById("catType").value; // coollink
                type = document.getElementById("appType").value;
                
                if(type == "application") {
                    place = `<%= certain.categories.find(o => o.link == cat).placeholder %>`;
                } else {
                    place = 'Topic description...';
                };
                document.getElementsByName('description')[1].innerHTML = place;
            };
</script>

The specific part I have a problem with is:

 place = `<%= certain.categories.find(o => o.link == cat).placeholder %>`;

I want to display cat, which is a WebJS Variable (that for now has a value of coollink) inside of my ExpressJS tags <%= coollink %> however I cannot seem to display it without it erroring and telling me it has an error with compiling it.

It works just fine if I change it to say

place = `<%= certain.categories.find(o => o.link == 'coollink').placeholder %>`;

but then it's not dynamic, and it needs to be dynamic for this project.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • You can set a Web JS variable = to an ExpressJS variable. But not the other way around, if there is a solution I would be greatly appreciative. – ThatGuyHyperz Aug 23 '22 at 20:32
  • Does this answer your question? [How to accessing client side javascript variable from server side nodejs?](https://stackoverflow.com/questions/27839872/how-to-accessing-client-side-javascript-variable-from-server-side-nodejs) – Heretic Monkey Aug 23 '22 at 20:41
  • Or [Get variable on client-side from the server-side (express.js, node.js)](https://stackoverflow.com/q/23031161/215552) – Heretic Monkey Aug 23 '22 at 20:42
  • I really would recommend you to make it easier by AJAX request so that you seperate logics and you minimize server side computations by a client request and server response to get this value – sohaieb azaiez Aug 23 '22 at 20:45

1 Answers1

-1

You can't do this.

The code inside <%= %> runs on the server side, the cat variable doesn't even exist yet.

What <%= %> do is place any string in this place. It doesn't even know it's a script.

The best you can do is:

<%= certain.categories %>.find(o => o.link == cat).placeholder

But it will expose all categories to the user

Konrad
  • 21,590
  • 4
  • 28
  • 64