Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call environment variables inside an ejs script

I need to use a process variable inside my ejs template in order to call an endpoint, but from this context I cannot reach the process nodejs variable.

How can I achieve this?

<a class="imgLink" href="#" onclick="get_user_info()">
    <div style="border: 2px solid gray;padding: 8px">Info</div>
</a>    
<script>
        function get_user_info() {
            $.get(`/users/${process.env.userId}`, function(data) {
                // Do something
            })
        }
</script>
like image 436
Julian Torregrosa Avatar asked Aug 13 '18 16:08

Julian Torregrosa


1 Answers

<a class="imgLink" href="#" onclick="get_user_info()">
    <div style="border: 2px solid gray;padding: 8px">Info</div>
</a>    
<script>
        function get_user_info() {
            var userId= '<%= process.env.userId %>';
            $.get(`/users/${userId}`, function(data) {
                // Do something
            })
        }
</script>

get the userId using <%= process.env.userId%>

like image 134
Edwin Babu Avatar answered Sep 30 '22 12:09

Edwin Babu