我的快速页面只加载一次,第二次失败



我有一个快速页面,当猫鼬数据库收到来自 jquery post 请求的 post 请求时,它会更新它,它确实如此,但只有一次,当我输入另一个文档时它失败了。我的猫鼬收藏中有两个文档,地点为温哥华。

j查询页面

$("#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帖子每次都会通过,因为开发人员工具控制台记录了设置字符串。有人可以解释为什么这段代码只运行一次吗?

当您

尝试覆盖vault模型时,您收到来自猫鼬的错误。

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

var stock = mongoose.model("vault",{
    name:{type:String},
    location:{type:String},
    minspent:{type:Number},
    minlength:{type:Number},
    member:{type:Boolean}
});
router.post('/logic',function(req,res,next){
    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 });
});

最新更新