2016-06-11 3 views
1

Ich habe eine Express-Seite, die ein Dokument in einer Mungo-Datenbank aktualisiert, wenn es eine Post-Anfrage von einer Anfrage jquery Post erhält, und es tut, aber nur einmal und wenn ich gehe ein anderes Dokument schlägt fehl. Meine Mungo-Sammlung hat zwei Dokumente in der Sammlung mit dem Standort als Vancouver.meine Express-Seite lädt nur einmal und schlägt das zweite Mal

jquery Seite

$("#sendover").click(function(){ 
 

 
\t \t var settings = JSON.stringify({ 
 
\t \t \t type : $("#type option:selected").text(), 
 
\t \t \t productname : $("#name").val(), 
 
\t \t \t collectionname : $("#groupings option:selected").text(), 
 
\t \t \t segment : $("#segment option:selected").text(), 
 
\t \t \t levels : $("#levels option:selected").text() 
 
\t \t }); 
 

 
\t \t console.log(settings); 
 

 
\t \t $.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:'); 
 
\t   console.log(err); 
 
      console.log(err.stack); 
 
     }).always(function() { 
 
     }); 
 
\t \t 
 
\t }); 
 
\t

Knoten js

router.post('/logic',function(req,res,next){ 
 
\t 
 
\t var stock = mongoose.model("vault",{ 
 
\t \t name:{type:String}, 
 
\t \t location:{type:String}, 
 
\t \t minspent:{type:Number}, 
 
\t  minlength:{type:Number}, 
 
\t \t member:{type:Boolean} 
 
\t }); 
 

 
\t if(req.body.type == 'collection'){ 
 
\t \t //edit corresponding attribute of all the items in that collection 
 
\t \t console.log("it is collection"); 
 
\t }else if(req.body.type == "item"){ 
 
\t \t //edit the corresponding attribute of the item 
 
\t \t product = req.body.productname; 
 
\t \t section = req.body.segment; 
 

 
\t \t stock.findOne({name:product}, function(err,doc){ 
 
\t \t \t if(err){console.log("failed to update doc");} 
 
\t \t \t doc.location = "The 6"; 
 
\t \t \t doc.save(); 
 
\t \t }); 
 
\t } 
 
\t console.log("it went through"); 
 
\t res.json({ ok: true }); 
 
\t \t 
 
});

die html, dass die Post gesendet

<form> 
 
\t <select id="type"> 
 
\t \t <option value="" disabled selected>content type</option> 
 
\t \t <option value="collection">collection</option> 
 
\t \t <option value="item">item</option> 
 
\t </select><br> 
 
\t <input type="text" id="name"><br> 
 
\t <select id ="groupings"> 
 
\t \t <option value = "collection1">Collection 1</option> 
 
\t \t <option value = "collection2">Collection 2</option> 
 
\t \t <option value = "collection3">Collection 3</option> 
 
\t \t <option value = "collection4">Collection 4</option> 
 
\t </select><br><br> 
 
\t <select id="segment"> 
 
\t \t <option value="location">certain location</option> 
 
\t \t <option value="minamount">Only if they've spent min-amount</option> 
 
\t \t <option value="minlength">min-duration member</option> 
 
\t \t <option value="existence">Only if they are members</option> 
 
\t </select><br><br> 
 
\t <select id="levels"> 
 
\t \t <option value="pictures">images</option> 
 
\t \t <option value="prices">prices</option> 
 
\t \t <option value="wholeitems">wholeitems</option> 
 
\t </select> 
 
\t <input type="submit" id="sendover">Set</input> 
 
</form>

das erste Mal, es funktioniert und ich dieses Konsolprotokoll

it went through 
POST /vaultsetup 200 62.787 ms - 11 

zum zweiten Mal um bekomme ich diese

POST /vaultsetup 500 17.834 ms - 1933 

und ich weiß, Der jquery-Post wird jedes Mal wegen der Entwickler-Tools-Konsolenprotokolle t durchlaufen Die Einstellungskette. Kann jemand erklären, warum dieser Code nur einmal ausgeführt wird?

+1

Das ist ein 500 interner Server Fehlerantwortcode. Was siehst du in deinen nodejs-Protokollen? – obi1

+0

Das ist da oben, die letzten 2 Zeilen des Konsolenprotokolls. Der Rest sind nur 304 Status – Sparksido

Antwort

0

Sie erhalten einen Fehler von Mongoose, da Sie versuchen, das Modell vault zu überschreiben.

Jedes Mal, wenn Sie eine POST-Anforderung an diesen Endpunkt senden, versuchen Sie, das Modell vault zu erstellen. Du darfst das nicht tun. Versuchen Sie, die Definition außerhalb der POST-Methode zu verschieben:

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 }); 
}); 
Verwandte Themen