Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript truepush回调读取_Javascript_Object_Foreach - Fatal编程技术网

Javascript truepush回调读取

Javascript truepush回调读取,javascript,object,foreach,Javascript,Object,Foreach,我有以下目标: . 通过下面的代码,我得到它并阅读它 truepush.push({ operation: "get-tags", callback: function(error,response){ console.log(error,response); subscribedTopics = []; response.data.forEach(el => subscribedT

我有以下目标:
.
通过下面的代码,我得到它并阅读它

    truepush.push({
  operation: "get-tags",
  callback: function(error,response){
               console.log(error,response);

               subscribedTopics = [];
               response.data.forEach(el => subscribedTopics.push(el.name));

               console.log("SUBSCRIBED LIST: " + subscribedTopics);

               subscribedTopics.forEach(function(el) {
                 if (el.name.equals("choir")) {
                   console.log("CHOIR CALLED");
                 }
                 else if (el.name.equals("kobel")) {
                   console.log("KOBEL CALLED");
                 }
                 else if (el.name.equals("kitchen")) {
                   console.log("KITCHEN CALLED");
                 }
               });
             }
  });
但我得到以下错误:

Uncaught TypeError: Cannot read property 'forEach' of undefined
at callback (index.js:1869)
at main.js:1
at XMLHttpRequest.i.onreadystatechange (main.js:1)  
它说错误在这一行:

response.data.forEach(el => subscribedTopics.push(el.name));
这里有什么问题

编辑:
这是我运行时得到的:
console.log(响应)

编辑2: 奇怪的是:当我打印subscribedTopics数组时(正如您在上面的代码示例中看到的那样)

…它返回我期望的正确数组。[“唱诗班”、“科贝尔”、“厨房”]

编辑3:
通过以下代码,我发现了一些奇怪的事情:

truepush.push({
  operation: "get-tags",
  callback: function(error,response){
               console.log("______________________");
               console.log("full output of console.log(error, response.data):");
               console.log(error, response.data);
               console.log("______________________");

               subscribedTopics = [];
               response.data.forEach(el => subscribedTopics.push(el.name));

               subscribedTopics.forEach(function(el) {
                 if (el.name.equals("choir")) {
                   console.log("CHOIR CALLED");
                   document.getElementById("switch_choir").checked = true;
                 }
                 else if (el.name.equals("kobel")) {
                   console.log("KOBEL CALLED");
                   document.getElementById("switch_kobel").checked = true;
                 }
                 else if (el.name.equals("kitchen")) {
                   console.log("KITCHEN CALLED");
                   document.getElementById("switch_kitchen").checked = true;
                 }
               });
             }
  });
以下是输出:


回调似乎触发了两次。首先使用正确的对象,然后在第二次触发时使用未定义的对象。


~Filip

看看代码和你得到的回复,它看起来不应该这样做。事实上,如果你正在排队:

console.log(“订阅列表:+subscribedTopics”)

那么您不可能在
response.data.forEach上失败


这让我相信,这个函数可能运行了两次——一次是在获取数据道具的地方,一次是在没有确认的时候,是否传递了所有正确的参数?onreadystatechange的状态正确吗?
响应。这里未定义数据。换句话说,您的响应对象没有
数据
property@James是的,但为什么?@ThomasBui您的意思是onreadystatechange处于正确的状态?console.log(error,response)
的输出是什么?抛出“Uncaught TypeError:response.forEach不是函数”。还有另一件奇怪的事情,你可以在我的编辑2中找到console.log(error,response.data)的输出是什么?似乎回调触发了两次。首先使用正确的对象,然后在第二次激发时使用未定义的对象。如果我在你的位置,我会调试并尝试找出为什么它会触发两次而不是一次,第二次是第二次响应。这听起来很合理,但它不起作用。控制台返回“UncaughtTypeError:无法读取未定义的属性'forEach'”作为我们昨天发现的同一行。但是我应该做什么来解决我的问题呢?如果不知道
truepush
的API/documentiun或调用它的前面代码,就很难确定。我会在代码中放置一个断点,以确定何时调用它,并查看为什么要调用两次此代码。
truepush.push({
  operation: "get-tags",
  callback: function(error,response){
               console.log("______________________");
               console.log("full output of console.log(error, response.data):");
               console.log(error, response.data);
               console.log("______________________");

               subscribedTopics = [];
               response.data.forEach(el => subscribedTopics.push(el.name));

               subscribedTopics.forEach(function(el) {
                 if (el.name.equals("choir")) {
                   console.log("CHOIR CALLED");
                   document.getElementById("switch_choir").checked = true;
                 }
                 else if (el.name.equals("kobel")) {
                   console.log("KOBEL CALLED");
                   document.getElementById("switch_kobel").checked = true;
                 }
                 else if (el.name.equals("kitchen")) {
                   console.log("KITCHEN CALLED");
                   document.getElementById("switch_kitchen").checked = true;
                 }
               });
             }
  });
try:
if(response.data !== 'undefined'){
    response.data.forEach(el => subscribedTopics.push(el.name));
}