I am developing a node.js application. I loaded a JSON file as app.locals.parameter in app.js. then I am trying to use it in my index.hbs.
Assume:
app.locals.componentData = require('./myfile.json');
when I used the parameter inside my HTML part as below it is functioning properly.
{{#each componentData.categories}}
<li id="{{this.categoryName}}" name="{{this.categoryName}}" class="btn btn-primary btn-sm" style="position: relative; z-index: 10; margin-bottom:3px" draggable="true" role="option" aria-grabbed="false"><div><img src="images/plugin.png" alt="{{this.categoryDescription}}"/>{{this.categoryName}}</div></li>
{{/each}}
but when I am trying to use the parameter in my javascript function it cannot recognize it.
function InitComponents() {
for( var i = 0; i < componentData.categories.length; i++ ){
alert("in loop!"+i+"-"+componentData.categories[i].categoryName);
if(componentData.categories[i].categoryName.match(itemId)){
alert("here:"+componentData.categories[i].parameters.length);
createParameterList(newID,componentData.categories[i].parameters);
}
}
}
my JSON format is as below:
{
"categoryName": "xyz",
"categories": [
{
"categoryName": "ABC",
"parameters": [
{
"ParameterID": "ID",
"ParameterName": "ID",
"ParameterDefultValue": "",
"ParameterValue": "",
"ParameterDescription": "ID Description"
}]
}]
}
may I know why system cannot understand componentData.categories.length in my script part?