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", });
- 无法使用节点js访问桌面应用程序中的窗口closuresfunction
- Mongoose findByIdAndUpdate / findById返回null作为模型
- 如何从另一个方法调用一个方法
- 在Jspm中使用非npm(legacy)javascript库
- 是否有专门为Node.js开发实践工作的客户端框架?
- backbone.js + node.js – express.js
- 将Backbone.iobind(socket.io)与一组node.js服务器一起使用
- authentication系统与node.js restify和backbone.js
- AJAX图像上传与backbone.js node.js和express