简体   繁体   English

在我的mean.js应用中添加控制器似乎会自动触发api调用?

[英]Adding a controller to my mean.js app seems to automatically fire an api call?

Having trouble tracking this down. 无法追踪到此。 I am adding a workflow to user signup in a meanjs app, such that an admin has to invite a user in order for them to be allowed to signup. 我正在将一个工作流添加到一个meanjs应用程序中的用户注册中,这样管理员必须邀请用户才能允许他们注册。

For some reason, whenever the invitation form loads, an API call is attempted to /api/users/invitation which I did not (as far as I know) ask for, and it doesn't make sense to have one as there's no data it needs. 出于某种原因,每当邀请表单加载时,都会尝试对我没有要求的/api/users/invitation进行API调用(据我所知),并且没有数据是没有意义的它需要。 I assume there's something being auto-wired for me somewhere, but since this 404 causes the page to fail, I need to kill it if I can. 我认为某处正在为我自动连线,但是由于此404导致页面失败,因此我需要将其杀死。

Here's my controller: 这是我的控制器:

    (function () {
  'use strict';

  angular
    .module('users.admin')
    .controller('InvitationController', InvitationController);

  InvitationController.$inject = ['$scope', '$state', '$window', 'Authentication'];

  function InvitationController($scope, $state, $window, Authentication) {
    var vm = this;
    vm.invitation = {};
    vm.sendMail = sendMail;
    vm.authentication = Authentication;

    function sendMail(isValid) {
      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'vm.invitationForm');

        return false;
      }

      var invitation = vm.invitation;

      // TODO: send to the server
      console.log(invitation);
    }
  }
}());

Here's the stateprovider fragment that's relevant: 这是相关的stateprovider片段:

.state('admin.user-invite',{
        url: '/users/invitation',
        templateUrl: 'modules/users/client/views/admin/invite-user.client.view.html',
        controller: 'InvitationController',
        controllerAs: 'vm',
        data: {
          pageTitle: 'Invite a User'
        }
      })

Any idea where else to look? 知道其他地方吗? This is my first app using the MEAN.js framework, though I've used angular quite a bit in the past. 这是我的第一个使用MEAN.js框架的应用程序,尽管过去我已经使用了很多角度。

OK, took me an embarrassingly long time to realize this, but it turns out the URL matching was the actual problem at hand. 好的,我花了很长时间尴尬地意识到这一点,但是事实证明URL匹配是当前的实际问题。

I should have included my full set of client-side routes in the question, as what was happening was that /users/invitation was actually the last route declared. 我应该在问题中包括我所有的客户端路由,因为发生的是/users/invitation实际上是最后声明的路由。 Turns out, /users/:userId was matching against that, and so the router was interpreting invitations as a userId, and the matching route had a resolver function which was in turn trying to call my server-side users api. 原来, /users/:userId匹配,因此路由器将invitations解释为userId,并且匹配的路由具有解析器功能,该功能随后试图调用我的服务器端用户api。

Now the part that is still baking my noodle (though it's less important in that my current problem is solved) is why would the route matcher do that, but still render the template that I'd assigned to the /users/invitation route? 现在仍在烘烤面条的部分(尽管它不那么重要,因为它可以解决我当前的问题)是为什么路线匹配器可以这样做,但仍呈现我分配给/users/invitation路线的模板? The fact that it was doing that certainly made the debugging longer, as the matched route has a very different template assigned to it than my intended route. 这样做的事实无疑会延长调试时间,因为匹配的路由分配给它的模板与我的预期路由非常不同。

Can you examine network requests in developer tools. 您可以在开发人员工具中检查网络请求吗? It could be a request to '/users/invitation', but the '/api' part is getting prefixed on the server side code (prefixed to router??). 它可能是对“ / users / invitation”的请求,但是“ / api”部分在服务器端代码(前缀为router ??)上得到了前缀。

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

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