0

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?

1 Answer 1

1

The problem here is that InitComponents function doesn't know anything about app.locals. app.locals variable is recognized inside templates. In order to make your code work you need to pass app.locals.componentData as a parameter to InitComponents function:

function InitComponents(componentData) {
     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);
          }
     }
}

Now you can call this function like this

InitComponents(app.locals.componentData);

and InitComponents will now actually know what componentData is. If you use this function inside a middleware you can access app via req.app:

myMiddlewareFunction((req, res, next) => {
    const app = req.app;
    initComponents(app.locals.componentData);
    // some other logic
})
Sign up to request clarification or add additional context in comments.

13 Comments

@volodymry: Thanks for your answer. I added changes as you said, but now in my button which called Initial component I received this error: "Uncaught ReferenceError: app is not defined." Also, can you explain the last part of your answer, I didnt get it. where I should add it?
@Amir can you please share the code of the function inside which you call InitComponents function?
@Volodymry: I call it like this: <button onclick="InitComponents()">Initialize Components</button> [previously] and now => <button onclick="InitComponents(app.locals.componentData)">Initialize Components</button>
I think, I misunderstood your question. I thought you are trying to use componentData inside JS on NodeJS side. But as far as I got it now you are trying to use componentData inside a frontend script. If so you need to fetch this JSON file from your server and to assign it to a variable inside your script. Or you can insert a <script> tag inside a template with javascript that will assign componentData to a global variable.
can you modify your answer based on your new understanding. because I am new to NodeJS. As I mentioned above I loaded the JSON in app.locals.componentData in app.js; I can use componentData successfully in my HTML part but in script part I dont know how to use it. :(
|

Your Answer

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.