Node JS Express - 更新文档

时间:2016-12-12 20:34:01

标签: javascript node.js express ejs

我通过http://localhost:3000/update2?buyerID=2299获取了一个获取特定文档的检索。

当我按提交时,它会重定向到/ orders页面,我可以在那里查看所有记录和当前字段。

如果我在检索中更改了一个字段并单击提交,则它不会更新文档与以前相同。

5个JSON文档中的1个 - http://prntscr.com/dilrb0

JS FILE

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', {qs: result});
                    }
                })
            }
        });
    }
});

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")
                    }
                })

            }
        });
    }
});

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>

1 个答案:

答案 0 :(得分:0)

我实际上对您在.post()开头仍然使用req.query.buyerID而不是req.body.buyerID这一事实感到困惑,但代码显然仍然达不到res.redirect(&#34; /订单&#34);首先,在你的updateOne中,尝试用parseInt(order.buyerID)替换parseInt(buyerID) - Chris G 10小时前