简体   繁体   English

Express JS路由更新问题

[英]Express js routing for update issue

I currently have a MEAN stack project and am trying to get the routing for updating work. 我目前有一个MEAN堆栈项目,正在尝试获取更新工作的路由。 Saving works fine. 保存工作正常。

Basically in my angular controller I have the following scope method where staff is the object sent on form submit: 基本上在我的角度控制器中,我有以下作用域方法,其中staff是在表单提交上发送的对象:

$scope.updateStaff = function(staff) {
    $scope.submitted = true;

    if (form.$valid) {
        staffActionService.update({ id: staff._id }, staff)
        .then(function () {
            $scope.success = true;
            console.log("successful");
        })
        .catch(function (err) {
            var err = err.data;
            $scope.errors = {};
            angular.forEach(err.errors, function (error, field) {
                form[field].$setValidity('mongoose', false);
                $scope.errors[field] = error.message;
            });
        });
    }
}

Also, I have a factory method which basically does the client side routing using $resource : 另外,我有一个工厂方法,基本上使用$resource进行客户端路由:

angular.module('xxx')
    .factory('staffActionService', function ($resource)  {
        return $resource('/api/staffs/:id/:controller',
            { id: '@_id' },
            { 'update': { method:'PUT' } }
        );
    });
});

My server side routing is set as follows 我的服务器端路由设置如下

var express = require('express');
var controller = require('./staff.controller');

var router = express.Router();

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id/staff', controller.update);
router.patch('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

But for some reason my server side is not being hit 但是由于某种原因,我的服务器端没有受到攻击

Any ideas as have tried a few things with the routing but no joy 任何想法都尝试了一些与路由有关的事情,但没有喜悦

Cheers 干杯

Looks like your calling the route for the PATCH method, but are using the PUT method on the client, which will result in a 404. 看起来您为PATCH方法调用了路由,但是在客户端上使用了PUT方法,这将导致404。

You can fix this in one of three ways: 您可以通过以下三种方式之一解决此问题:

You can fix it in express by making the PUT route respond to '/:id': 您可以通过使PUT路由响应'/:id'来以快速方式修复它:

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id/staff', controller.update);
router.put('/:id', controller.update); // add this
router.patch('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

OR you can fix it in your angular controller by defining the ':controller' from the $resource factory you set up: 或者,您可以通过在您设置的$ resource工厂中定义':controller'来在角度控制器中对其进行修复:

staffActionService.update({ id: staff._id, controller: 'staff' }, staff)

OR you can fix it in your $resource factory by changing the method to PATCH: 或者,您可以通过将方法更改为PATCH来在$ resource工厂中对其进行修复:

angular.module('xxx')
  .factory('staffActionService', function ($resource)  {
    return $resource('/api/staffs/:id/:controller', { id: '@_id' }, {'update': {method:'PATCH'}})
  });

Note: don't combine the above examples, use only one! 注意:请勿将以上示例结合使用,只能使用一个示例!

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

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