I want to access a JSON array inside the script tag of a EJS file. I'm passing the JSON array from my index.js file to the EJS file while rendering it. The code is:
sampleJArr=["data1","data2","data3"];
app.get('/test', function(req,res){
res.render('testfile',{
'mJsonArr': sampleJArr
});
In the EJS file (testfile.ejs), code-:
<div>
<!--Printing the JSON array. It prints -->
<p>JSON.stringify(mJsonArr)</p>
<button onClick="myFunc">Take test</button>
</div>
<script>
function myFunc(){
var JsonData= <%= mJsonArr[0] %>
console.log(JsonData)
}
</script>
Error:-Uncaught ReferenceError: myFunc is not defined at HTMLButtonElement.onclick
But when I'm trying to do a console("some string") inside myFunc then it's not giving that error. Only when I'm accessing mJsonArr inside the script tag, I'm getting UncaughtRefrence error. How can I access the passed mJsonArr inside my script tag ? Please contribute. Thankyou.