简体   繁体   English

TypeError:无法读取未定义[VueJs]的属性“ push”

[英]TypeError: Cannot read property 'push' of undefined [VueJs]

I am trying to add an object to an array but it is not working with me, the program can't read the property push 我正在尝试将对象添加到数组,但无法正常工作,程序无法读取属性推送

I defined an array in <script> : 我在<script>定义了一个数组:

Data: function() {
  return {
     Projects: [
        {
           name: '',
           id: 0,
           subscribers: 0,
           products: {name:'',color:''},
        }
     ],
}

And in the function: 并在函数中:

GetAllWorkspaces: function(){

   var app = this;

  const instance = axios.create({
       timeout: 1000,
       headers: {
              ........
            }
          });
  instance.get("XXXXXXX")
  .then( function(response) {
  console.log(response);
  Object.keys(response.data.result).forEach( function (product) {

      var subscribersCounter = 0;

      let example = {
         name: response.data.result[product].name,
         id: response.data.result[product].id,
         subscribers: response.data.result[product].subscribers,
         products: response.data.result[product].products,
      };

      let uploadedExample = {
         name: '',
         id: '',
         subscribers: '',
         products: {name:'',color:''},
      };

      uploadedExample.name = example.name;
      uploadedExample.id = example.id;

      if ( example.subscribers ) {
      Object.keys(example.subscribers).forEach(function (key) {
          subscribersCounter++;
      });
      }

      uploadedExample.subscribers = subscribersCounter;

      if ( example.products ) {
         Object.keys(example.products).forEach(function (Pkeys) {
            uploadedExample.products.name = Pkeys;
            Object.keys(example.products[Pkeys]).forEach(function (key) {
                if (key == 'color') {
                        uploadedExample.products.color = example.products[Pkeys][key];
                }
             });
          });
        }

        //add the new workspace to the list of workspaces.
        app.Projects.push(uploadedExample);

    });

    })
    .catch(function(error) {
        console.log(error);
     });

My problem is with this line 我的问题是这条线

app.Projects.push(uploadedExample); app.Projects.push(uploadedExample);

where when I try to push an object into the array, the error message is shown: 当我尝试将对象推入数组时,将显示错误消息:

TypeError: Cannot read property 'push' of undefined TypeError:无法读取未定义的属性“ push”

As the error says, the problem is that app.Projects is undefined. 如错误所述,问题是app.Projects未定义。 This happens because 'this' refers to the function scope inside GetAllWorkspaces and not to the component scope (you can try it by console.logging 'this' - anyway- it is a good practice under all circumstances because 'this' can change from context to context). 发生这种情况是因为'this'是指GetAllWorkspaces内的函数作用域,而不是组件范围(您可以通过console.log尝试使用它。无论如何,这是一个好习惯,因为'this'可以随上下文而变化上下文)。 If you want to keep the component scope inside the method, you should use an arrow function like this: 如果要将组件范围保留在方法中,则应使用如下箭头函数:

GetAllWorkspaces: () => {
// do all your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM