它与我的网址的/:id
结尾有关;我想,我无法让我的Mongoose和Backbone ID字段正确匹配。这是完整的控制台错误POST http://localhost:8080/api/bears/:id 404 (Not Found)
以下是我save()
模型的视图。
var HomeView = Backbone.View.extend({
el:$('#main'),
render: function(){
this.template = _.template($('#home_template').html());
this.$el.html(this.template);
$('#new-entry').submit(function(ev){
var entry = new Entry({task: $('#word').val(), description: $('#definition').val() });
dictionary.add(entry);
entry.save();
console.log(dictionary.toJSON());
$('#body').children('input').val('');
return false;
});
}
})
这是我的猫鼬模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectID = Schema.ObjectID;
var EntrySchema = new Schema({
task: String,
description: String
});
module.exports = mongoose.model('Entry', EntrySchema);
Mongoose .post()
路线我定位:
router.route('/bears')
//create a bear
.post(function(req, res){
var entry = new Entry();
entry.task = req.body.task;
entry.description = req.body.description;
entry.save(function(err){
if(err)
res.send(err);
res.json({message: 'task created'});
})
});
这是我的模型定义:
var Entry = Backbone.Model.extend({
urlRoot: '/api/bears/',
defaults: function(){
return{
task: '',
description: ''
}
},
parse: function(response){
response.id = response._id;
return response;
},
idAttribute: "_id",
});
答案 0 :(得分:2)
您需要在模型中设置urlRoot:
var Entry = Backbone.Model.extend({
urlRoot: '/api/bears/',
defaults: function(){
return{
task: '',
description: ''
}
},
parse: function(response){
response.id = response._id;
return response;
},
idAttribute: "_id",
});