【发布时间】:2016-06-11 15:17:52
【问题描述】:
我有一个快速页面,当它收到来自 jquery 发布请求的发布请求时,它会更新 mongoose 数据库中的文档,它确实如此,但只有一次,当我输入另一个文档时它会失败。我的 mongoose 集合中有两个文档,位置为温哥华。
jquery 页面
$("#sendover").click(function(){
var settings = JSON.stringify({
type : $("#type option:selected").text(),
productname : $("#name").val(),
collectionname : $("#groupings option:selected").text(),
segment : $("#segment option:selected").text(),
levels : $("#levels option:selected").text()
});
console.log(settings);
$.ajax('/logic', {
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: settings
}).done(function () {
console.log('message saved successfully');
}).fail(function (err) {
console.log('failed to save message:');
console.log(err);
console.log(err.stack);
}).always(function () {
});
});
节点js
router.post('/logic',function(req,res,next){
var stock = mongoose.model("vault",{
name:{type:String},
location:{type:String},
minspent:{type:Number},
minlength:{type:Number},
member:{type:Boolean}
});
if(req.body.type == 'collection'){
//edit corresponding attribute of all the items in that collection
console.log("it is collection");
}else if(req.body.type == "item"){
//edit the corresponding attribute of the item
product = req.body.productname;
section = req.body.segment;
stock.findOne({name:product}, function(err,doc){
if(err){console.log("failed to update doc");}
doc.location = "The 6";
doc.save();
});
}
console.log("it went through");
res.json({ ok: true });
});
发送帖子的 html
<form>
<select id="type">
<option value="" disabled selected>content type</option>
<option value="collection">collection</option>
<option value="item">item</option>
</select><br>
<input type="text" id="name"><br>
<select id ="groupings">
<option value = "collection1">Collection 1</option>
<option value = "collection2">Collection 2</option>
<option value = "collection3">Collection 3</option>
<option value = "collection4">Collection 4</option>
</select><br><br>
<select id="segment">
<option value="location">certain location</option>
<option value="minamount">Only if they've spent min-amount</option>
<option value="minlength">min-duration member</option>
<option value="existence">Only if they are members</option>
</select><br><br>
<select id="levels">
<option value="pictures">images</option>
<option value="prices">prices</option>
<option value="wholeitems">wholeitems</option>
</select>
<input type="submit" id="sendover">Set</input>
</form>
第一次,它成功了,我得到了这个控制台日志
it went through
POST /vaultsetup 200 62.787 ms - 11
我第二次得到这个
POST /vaultsetup 500 17.834 ms - 1933
而且我知道 jquery 帖子每次都会通过,因为开发人员工具控制台会记录设置字符串。谁能解释一下为什么这段代码只运行一次?
【问题讨论】:
-
这是一个 500 内部服务器错误响应代码。您在 nodejs 日志中看到了什么?
-
就是这样,控制台日志的最后两行。剩下的只有 304 个状态