7

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>
asked Aug 13, 2018 at 16:37
1
  • 1
    Just a remark for best practice, your Environment variables shoul have capitalized name, in your case: 'uSER-ID' . Commented Jul 24, 2020 at 6:24

1 Answer 1

18
<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%>

answered Aug 13, 2018 at 18:53
Sign up to request clarification or add additional context in comments.

2 Comments

It works but <%= process.env.userId %> should have quotes var userId= '<%= process.env.userId %>';
What about just $.get('/users/<%= process.env.userId %>', function (data) { ... })?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.