骨干网和快递:上res.redirect
问题描述:
我有一个动作,我需要更新的MongoDB条目包括_id
领域,这需要删除旧的条目,使一个新的concatinating(复制)的路线,这里是服务器端:骨干网和快递:上res.redirect
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.save
和model.destroy
或防止URL
。任何意见如何做到这一点表示赞赏,谢谢。
编辑 这里只是一个错字的错误,是不相关的问题,盲目检查路线,看到的取消
答
我相信这个问题是在这里:
res.redirect('admin/account-groups/' + outcome.newId + '/');
这是一个相对路径所以它会被附加到当前的URL上。我怀疑你想要这样的东西:
res.redirect('/admin/account-groups/' + outcome.newId + '/');
你发布了两个相同的代码行,我没有真正明白你的意思。你能详细说明一下吗? – ASem
它们不相同。在第二个路径的开头有一个额外的'/'。 – skirtle
这是一个错误,谢谢 – ASem