简体   繁体   English

无法读取未定义的属性。 如何定义?

[英]Cannot read property of undefined. How to define?

I am trying to run a custom PUT in Angularjs, however I cannot for the life of me get it to work. 我试图在Angularjs中运行自定义PUT,但是我终生无法正常工作。 I need both UserResource and user to be defined but only the parameter in the first position of the update function gets defined. 我需要同时定义UserResource和user,但是仅定义了update函数第一位置的参数。 What can I do to define both user and UserResource? 如何定义用户和UserResource?

(function () {

class AdminController {
  constructor(User, Auth) {
    this.users = User.query();
    this.Auth = Auth;
  }

//here user is defined and I can run the delete function
  delete(user) {
    user.$remove();
    this.users.splice(this.users.indexOf(user), 1);
  }

// and if I put user into the first position it is defined but UserResource isn't 
// as written here it isn't defined either. 
  update(UserResource, user) {
    var $id = user._id;
    UserResource.update({ id:$id }, user);
  }
}

angular.module('wcsdesktopApp.admin')
  .controller('AdminController', AdminController);
})();

EDIT to add more information 编辑以添加更多信息

I call the update function like this: 我这样调用更新函数:

  <table>
    <tr>
      <a ng-click="admin.update(user)" class="update"><span class="fa fa-refresh fa-2x"></span></a>
    </tr>
    <tr>
      <a ng-click="admin.delete(user)" class="trash"><span class="fa fa-trash fa-2x"></span></a>
    </tr>
  </table>

I included the delete function for reference since that is working. 我包括删除功能以供参考,因为它可以正常工作。

UserResource is an AngularJS $resource custom put request. UserResource是AngularJS $ resource自定义放置请求。

(function() {

function UserResource($resource) {
  return $resource('/api/users/:id/:controller', {
    id: '@_id'
  }, {
    changePassword: {
      method: 'PUT',
      params: {
        controller: 'password'
      }
    },
    update: {
      method: 'PUT'
    },
    get: {
      method: 'GET',
      params: {
        id: 'me'
      }
    }
  });
}

angular.module('wcsdesktopApp.auth')
  .factory('User', UserResource);

})();

You need to make sure that you are injecting your UserResource service into your controller. 您需要确保将UserResource服务注入到控制器中。 One of the many ways would be like the below: 许多方式之一如下所示:

angular.module('wcsdesktopApp.admin').controller('AdminController', AdminController);
AdminController.$inject = [ 'User'];
function AdminController(User) {}

Doing that would then change your update signature in your controller to the following: 然后,将控制器中的update签名更改为以下内容:

update(user) {
    User.update(); // Do whatever update.
}

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

相关问题 无法读取未定义的属性“ removeClass”。 Java脚本 - Cannot read property “removeClass” of Undefined. Javascript “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 无法读取未定义的属性“长度”。” - Cannot read property 'length' of undefined." 无法读取未定义的属性“indexOf”。 为什么是未定义的? - Cannot read property 'indexOf' of undefined. Why is it undefined? 无法读取未定义的属性。 但属性是进口 class 的 static function - Cannot read property of undefined. But property is a static function of an imported class 无法从未定义中读取属性“ 0”。 错误的Google Apps脚本 - Cannot read property “0” from undefined. Error Google Apps Script TypeError:无法读取未定义的属性“ get”。 是npm的问题吗? - TypeError: Cannot read property 'get' of undefined. Is npm the issue? 无法读取未定义的属性“ x”。 json文件中的第二级 - Cannot read property 'x' of undefined. second level in json file 无法读取未定义的属性“ handle”。 Parse.com迁移 - Cannot read property 'handle' of undefined. Parse.com migration 无法读取未定义的属性“ push”。 将数据添加到数组。 - Cannot read property 'push' of undefined. adding data to an array.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM