2016-12-12 123 views
0

我遇到了在我的快速應用程序內實施更新的問題。 http://localhost:3000/update2?buyerID=2299 當我輸入上面顯示的鏈接,它將我帶到一個表格,但它的空... 我想這個表格填充與買方ID文件相關的當前值。節點JS Express更新問題

任何人都可以看到我要去哪裏錯了嗎?

感謝

JSON文件

http://prntscr.com/dil1fj

JS文件

router.get('/update2', function(req, res) { 
    var buyerID = req.query.buyerID; 

    if (!buyerID || !parseInt(buyerID)) { 
     res.render('error', {message: "You need to enter an ID to update a specific order", error:{status:"", stack: ""}}); 
    } else { 

     mongoClient.connect(url, function (err, db) { 
      if (err) { 
       res.render('error', {message: "Failed to connect to MongoDB",error:{status:"", stack: ""}}); 
      } else { 
       var WishList = db.collection('orders'); 
       WishList.findOne({"buyerID": parseInt(buyerID)}, function(err, result) { 
        if (err || !result || result.length == 0) { 
         res.render('error', {message: "Failed to find order",error:{status:"", stack: ""}}); 
        } else { 
         res.render('updateOrder2', {updateOrder2: result, qs: req.query}); 
        } 
       }) 
      } 
     }); 
    } 
}); 

router.post('/update2', function(req, res) { 
    var buyerID = req.query.buyerID; 

    if (!buyerID || !parseInt(buyerID)) { 
     res.render('error', {message: "Failed to update order make sure the ID is current", error:{status:"", stack: ""}}); 
    } else { 

     mongoClient.connect(url, function (err, db) { 
      if (err) { 
       res.render('error', {message: "Failed to connect to MongoDB", error:{status:"", stack: ""}}); 
      } else { 
       var WishList = db.collection('orders'); 

       var order = { 
        buyerID: req.body.buyerID, 
        gender: req.body.gender, 
        student: req.body.student, 
        pName: req.body.pName, 
        type: req.body.type, 
        purchaseDate: req.body.purchaseDate, 
        price: req.body.price, 
        sName: req.body.sName 

       }; 
       console.log("Updating..", order) 
       WishList.updateOne({"buyerID": parseInt(buyerID)}, {$set: order}, function (err, result){ 
        if (err) { 
         console.log("Cannot update", err) 
        } else { 
         console.log("Updated..", order) 
         res.redirect("/orders") 
        } 
       }) 

      } 
     }); 
    } 
}); 

updateOrder2.ejs

<!DOCTYPE html> 
<html> 
<head> 
    <title>Update Recipe</title> 
    <link rel='stylesheet' href='/stylesheets/style.css' /> 
</head> 
<body> 

<form name= "updateOrder" action="/updateOrder" method="POST"> 
    User ID:<br> 
    <input type="text" name="buyerID" value = <%=qs.buyerID%>/ required ="required"><br> 
    Gender:<br> 
    <input type="text" name="gender" value = <%=qs.gender%>/ required ="required"><br> 
    Student?:<br> 
    <input type="text" name="student" value = <%=qs.student%>/ required ="required"><br> 
    Product ID:<br> 
    <input type="text" name="productID" value = <%=qs.productID%>/ required ="required"><br> 
    Product Name:<br> 
    <input type="text" name="pName" value = <%=qs.pName%>/ required ="required"><br> 
    Product Type:<br> 
    <input type="text" name="type" value = <%=qs.type%>/ required ="required"><br> 
    Date Purchased:<br> 
    <input type="text" name="purchaseDate" value = <%=qs.purchaseDate%>/ required ="required"><br> 
    Product Price:<br> 
    <input type="text" name="price" value = <%=qs.price%>/ required ="required"><br> 
    Seller Name:<br> 
    <input type="text" name="sName" value = <%=qs.sName%>/ required ="required"><br> 
    <br><br> 



    <input type="submit" value="Submit" /> 
</form> 


</body> 
</html> 
+0

當您在瀏覽器中查看空表單的源代碼時,您會看到什麼?您在節點控制檯中是否收到錯誤? –

+0

即時通訊沒有得到任何錯誤,我只是希望字段填寫與該買家ID有關的數據的形式 –

+0

我從來沒有使用ejs,但我敢肯定你a)需要至少把值包含引號中的空格和b)刪除斜槓。另外,你認爲什麼是源代碼?它是否包含「value =」?如果是這樣,在那之後插入了什麼?另外,你將'qs:req.query'傳遞給渲染器。你不應該通過'qs:result'嗎? –

回答

0

我從來沒有使用ejs,但我敢肯定你a)至少需要在引號中包含空格的值,並b)刪除斜槓。另外,你認爲什麼是源代碼?它是否包含「value =」?如果是這樣,在那之後插入了什麼?另外,您將qs: req.query傳遞給渲染器。你不應該通過qs: result

關於更新,我猜這在router.post('/update2'開始是問題;你需要使用req.body.buyerID。我建議在關鍵線上向您的代碼添加大量console.log()命令。

+0

字段的文件,如果你可以看看我的最新問題,我也可以找出這一個。 –

0

我在猜這個var buyerID = req.query.buyerID;在router.post的開頭('/ update2'是問題;你需要使用req.body.buyerID。我建議在關鍵的代碼中添加大量的console.log()命令到你的代碼中 - Chris G 4分鐘前