jquery - my express page only loads once and fails the second time -


i have express page updates doc in mongoose database when receives post request jquery post request, , does, once , when enter doc fails. mongoose collection has 2 docs in collection location vancouver.

jquery page

$("#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 save message:');  	        console.log(err);              console.log(err.stack);          }).always(function () {          });  		  	});  	

node 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 items in collection  		console.log("it collection");  	}else if(req.body.type == "item"){  		//edit corresponding attribute of item  		product = req.body.productname;  		section = req.body.segment;    		stock.findone({name:product}, function(err,doc){  		 	if(err){console.log("failed update doc");}  		 	doc.location = "the 6";  		 	doc.save();  		});  	}  	console.log("it went through");  	res.json({ ok: true });  		  });

the html sent post

<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 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>

the first time around, works , console log

it went through post /vaultsetup 200 62.787 ms - 11 

the second time around this

post /vaultsetup 500 17.834 ms - 1933 

and know jquery post goes through each time because of developer tools console logs settings string. can explain why code runs once?

you getting error mongoose trying overwrite vault model.

every time send post request endpoint trying create vault model. you're not allowed this. try moving definition outside of post method:

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 items in collection         console.log("it collection");     } else if (req.body.type == "item") {         //edit corresponding attribute of item         product = req.body.productname;         section = req.body.segment;          stock.findone({name:product}, function(err,doc) {             if(err){console.log("failed update doc");}             doc.location = "the 6";             doc.save();         });     }     console.log("it went through");     res.json({ ok: true }); }); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -