我的快递页面只加载一次而第二次失败

时间:2016-06-11 00:36:17

标签: jquery node.js mongodb

我有一个快速页面,当它从jquery post请求收到一个post请求时更新一个mongoose数据库中的doc,但是它确实只有一次,当我输入另一个doc时它失败了。我的猫鼬系列有两个文档,位于温哥华。

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>
&#13;
&#13;
&#13;

第一次,它工作,我得到这个控制台日志

it went through
POST /vaultsetup 200 62.787 ms - 11

第二次我得到这个

POST /vaultsetup 500 17.834 ms - 1933

我知道每次都会发布jquery帖子,因为开发人员工具控制台会记录设置字符串。有人能解释为什么这段代码只运行一次吗?

1 个答案:

答案 0 :(得分:0)

当您尝试覆盖MStream模型时,您收到来自Mongoose的错误。

每次向该端点发送POST请求时,您都会尝试创建vault模型。你不允许这样做。尝试在POST方法之外移动定义:

vault