gpt4 book ai didi

javascript - 主干 .save() 上的 404 错误

转载 作者:行者123 更新时间:2023-11-30 17:06:27 25 4
gpt4 key购买 nike

这与我的网址的/:id结尾有关;我想我无法让我的 Mongoose 和 Backbone ID 字段正确匹配。这是完整的控制台错误 POST http://localhost:8080/api/bears/:id 404 (Not Found)

这是我在其中save() 我的模型的 View 。

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() 路线我的定位:

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

关于javascript - 主干 .save() 上的 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27928250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com