Connecting different script tags

Without seeing your code or knowing what you've tried to debug it - no, I don't.

Assuming you haven't changed the code you shared previously, then I can see you build up the options array in the response handler for the fetch, but you never do the work to update those options in the select box.

Your code does have the for loop at the start that loops over options - but at that point in time, the fetch hasn't happened yet, so options will be empty.

    fetchUrl();
      for (let i = 0; i < options.length; i++) {
        let value = options[i].v;
        let text = options[i].t;
        $("#node-input-options").append(
          $("<option></option>").attr("value", value).text(text)
        );
      }
      $("#node-input-options").val(this.options);
      $("#node-input-server").val(this.server);
    },

    label: function () {
      let options = this.options;
      let optionText = "";

      for (let i = 0; i < options.length; i++) {
        if (options[i].v === this.options) {
          optionText = options[i].t;
          break;
        }
      }
      return this.name || "Custom Get";
    },

I have this code after the fetch request. I guess the problem is somewhere in there.

The call to fetchUrl() will return before the request has completed - so when that for loop runs, options will still be empty.

You need to do the for loop inside the callback of the fetch request.


        fetch(url)
          .then((res) => res.json())
          .then((data) => {
            opt = data;
            //console.log(opt);

            console.log(options);
            opt.forEach(myFunction);

            function myFunction(item) {
              // console.log(item);
              options.push({ v: item.address, t: item.name });
              console.log(options);
            }
          });
        for (let i = 0; i < options.length; i++) {
          let value = options[i].v;
          let text = options[i].t;
          $("#node-input-options").append(
            $("<option></option>").attr("value", value).text(text)
          );
        }
      };

      fetchUrl();

      $("#node-input-options").val(this.options);
      $("#node-input-server").val(this.server);
    },

    label: function () {
      let options = this.options;
      let optionText = "";

      for (let i = 0; i < options.length; i++) {
        if (options[i].v === this.options) {
          optionText = options[i].t;
          break;
        }
      }
      return this.name || "Custom Get";
    },
  });

Something like this? Its inside the fetch function but it still doesnt display anything.Probably i did somehting wrong .Ill figure it out.

Nevermind. I made it work. Thanks for the help .

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.