404骨干错误.save()

它与我的URL的/: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; }); } }) 

这是我的mongoose模式:

 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()路线Im瞄准:

 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", }); 

你需要在你的模型中设置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", });