我在快递应用程序中实现更新时遇到问题。 http://localhost:3000/update2?buyerID=2299 当我进入上面显示的链接时,它会将我带到一个表格,但它是空的...... 我希望使用与买方ID文档相关的当前值填充此表单。
谁能看到我哪里出错?
由于
json文件
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)
我猜这个var buyerID = req.query.buyerID;在router.post的开头('/ update2'是问题;你需要使用req.body.buyerID。我建议在关键行添加大量的console.log()命令到你的代码。 - Chris G 4分钟前
答案 1 :(得分:0)
我从未使用过ejs,但我很确定你a)需要至少在引号中包含空格的值,并且b)删除斜杠。另外,您认为源代码是什么?它包含“value =”吗?如果是这样,之后会插入什么?此外,您正在将qs: req.query
传递给渲染器。你不应该通过qs: result
吗?
关于更新,我猜测var buyerID = req.query.buyerID;
开头router.post('/update2'
是问题所在;你需要使用req.body.buyerID
。我建议在关键行添加大量console.log()
命令。