Backbone和Express:res.redirect上的concatinating(复制)路由

时间:2017-10-15 23:47:01

标签: node.js express backbone.js

我有一个操作,我需要更新MongoDB条目,包括_id字段,这需要删除旧条目并创建一个新条目,这是服务器端:

exports.update = function(req, res, next){
  var outcome = [];

  outcome.previousId = req.params.id;
  outcome.newId = req.body.name;

  var getPreviousRecord = function(callback) {
    req.app.db.models.AccountGroup
      .findOne({ _id: req.params.id })
      .lean()
      .exec(function(err, accountGroups) {
        if (err) {
          return callback(err, null);
        }

        outcome.accountGroups = accountGroups;
        return callback(null, 'done');
      });
  };

  var makeNewRecord = function(callback) {
    var permissions = outcome.accountGroups.permissions;
    var fieldsToSet = {
      _id: outcome.newId.toLowerCase(),
      name: outcome.newId,
      permissions: permissions
    };

    req.app.db.models.AccountGroup
      .create(fieldsToSet, function(err, record) {
        if (err) {
          return callback(err, null);
        }

        outcome.record = record;
        return callback(null, 'done');
      });
  };

  var deletePreviousRecord = function() {
    req.app.db.models.AccountGroup
      .findByIdAndRemove(outcome.previousId)
      .exec(function(err) {
        if (err) {
          return next(err);
        }

        res.redirect('admin/account-groups/' + outcome.newId + '/');
      });
  };

  var asyncFinally = function(err) {
    if (err) {
      return next(err);
    }
  };

  require('async').series([getPreviousRecord, makeNewRecord, deletePreviousRecord], asyncFinally);
};

它工作正常,但我无法在前端正常工作,它返回旧路线和新路线,例如:

PUT /admin/account-groups/customers22/admin/account-groups/Customers2233/ 404 213.749 ms - 31

其中customers22已过时_id而customers2233为新_id。如果我从另一个页面导航到新条目,它会正常获取路径。

在客户端:

(function() {
  'use strict';

  app = app || {};

  app.Details = Backbone.Model.extend({
    idAttribute: '_id',
    defaults: {
      success: false,
      errors: [],
      errfor: {},
      name: ''
    },
    url: function() {
      return '/admin/account-groups/'+ app.mainView.model.id +'/';
    },
    parse: function(response) {
      if (response.accountGroup) {
        app.mainView.model.set(response.accountGroup);
        delete response.accountGroup;
      }

      return response;
    }
  });

  app.DetailsView = Backbone.View.extend({
    el: '#details',
    events: {
      'click .btn-update': 'update'
    },
    template: Handlebars.compile( $('#tmpl-details').html() ),
    initialize: function() {
      this.model = new app.Details();
      this.syncUp();
      this.listenTo(app.mainView.model, 'change', this.syncUp);
      this.listenTo(this.model, 'sync', this.render);
      this.render();
    },
    syncUp: function() {
      this.model.set({
        _id: app.mainView.model.id,
        name: app.mainView.model.get('name')
      });
    },
    render: function() {
      this.$el.html(this.template( this.model.attributes ));

      for (var key in this.model.attributes) {
        if (this.model.attributes.hasOwnProperty(key)) {
          this.$el.find('[name="'+ key +'"]').val(this.model.attributes[key]);
        }
      }
    },
    update: function() {
      this.model.save({
        name: this.$el.find('[name="name"]').val()
      });
    }

  });

  app.MainView = Backbone.View.extend({
    el: '.page .container',
    initialize: function() {
      app.mainView = this;
      this.model = new app.AccountGroup( JSON.parse( unescape($('#data-record').html()) ) );

      // ...
      app.detailsView = new app.DetailsView();
    }
  });

  $(document).ready(function() {
    app.mainView = new app.MainView();
  });   
}());

可能需要同时触发model.savemodel.destroy或阻止使用URL。如何做任何建议表示赞赏,谢谢。

修改 这里只是一个与问题无关的拼写错误,鲁莽地检查路线,视为已取消

1 个答案:

答案 0 :(得分:2)

我认为问题在于:

res.redirect('admin/account-groups/' + outcome.newId + '/');

这是一个相对路径,因此它将被附加到当前URL。我怀疑你想要这样的东西:

res.redirect('/admin/account-groups/' + outcome.newId + '/');