我想什么时候'edit=true‘一个表单与预先填充的数据应该呈现,否则,只是重定向我。但我一直被重定向,并得到控制台错误“无法读取未定义的属性(读取”标题“)-这是我的价格、imagUrl等在我的pug文件中发生的。
管理Js文件夹
const Product = require('../models/product');
exports.getAddProduct = (req, res, next) => {
//res.render(path.join(rootDir, 'views', '/add-product.pug'))
res.render('admin/edit-product',
{pageTitle: 'Add Product',
path: '/admin/add-product',
editing: false
});
}
exports.postAddProduct= (req, res, next) => {
const title = req.body.title;
const imageUrl = req.body.imageUrl;
const price = req.body.price;
const description = req.body.description;
const product = new Product(title, imageUrl, description, price);
product.save();
res.redirect('/');
};
exports.getEditProduct = (req, res, next) => {
const editMode = req.query.edit;
if (!editMode) {
return res.redirect('/');
}
const prodId = req.params.productId;
Product.findById(prodId, product => {
if (!product) {
res.redirect('/')
}
res.render('admin/edit-product',
{pageTitle: 'Edit Product',
path: '/admin/edit-product',
editing: editMode,
product: product
});
});
};
exports.getProducts = (req, res, next) => {
Product.fetchAll((products) => {
res.render('admin/products', {
prods: products,
pageTitle: 'Admin Products',
path: '/admin/products'
});
});
}
products.js文件
const fs = require('fs');
const path = require('path');
const products = [];
const p = path.join(
path.dirname(process.mainModule.filename),
'data',
'products.json'
);
const getProductsFromFile = cb => {
fs.readFile(p, (err, fileContent) => {
if (err) {
cb([]);
} else {
cb(JSON.parse(fileContent));
}
});
};
module.exports = class Products {
constructor(title, imageUrl, description, price) {
this.title = title;
this.imageUrl = imageUrl;
this.description = description;
this.price = price;
}
save() {
this.id = Math.random().toString();
getProductsFromFile(products => {
products.push(this);
fs.writeFile(p, JSON.stringify(products), (err) => {
console.log(err);
});
});
};
static fetchAll(cb) {
getProductsFromFile(cb)
}
static findById(id, cb) {
getProductsFromFile(products => {
const product = products.find(p => p.id === id);
cb(product);
})
}
};
edit-product.pug
extends ../layouts/main-layout.pug
block styles
link(rel="stylesheet", href="/css/product.css")
link(rel="stylesheet", href="/css/forms.css")
block content
main
.main-form
if editing
form(action="/admin/edit-product", method="POST")
.main-input
label(for="title") Title
input(type="text", name="title", placeholder="Search Product", value=product.title)
.main-input
label(for="imageUrl") Image URL
input(type="url", name="imageUrl", id="imageUrl", value=product.imageUrl)
.main-input
label(for="price") Price
input(type="number", name="price", id="price", step="0.01", value=product.price)
.main-input
label(for="description", value=product.description) Description
textarea(name="description", id="description", rows="5")
.main-button
if editing
button.btn(type="submit") Update Product
else
button.btn(type="submit") Add Product
else
form(action="/admin/add-product", method="POST")
.main-input
label(for="title") Title
input(type="text", name="title", placeholder="Search Product")
.main-input
label(for="imageUrl") Image URL
input(type="url", name="imageUrl", id="imageUrl")
.main-input
label(for="price") Price
input(type="number", name="price", id="price", step="0.01")
.main-input
label(for="description") Description
textarea(name="description", id="description", rows="5")
.main-button
if editing
button.btn(type="submit") Update Product
else
button.btn(type="submit") Add Product
product.pug文件
extends ../layouts/main-layout.pug
block styles
link(rel="stylesheet", href="/css/main.css")
link(rel="stylesheet", href="/css/card.css")
block content
main
if prods.length > 0
.grid
each product in prods
article.product-item
header.card__header
h1.product__title #{product.title}
.card__image
img(src=`${product.imageUrl}`
alt="#{product.title}")
.card__content
h2.product__price #{product.price}
p.product__description #{product.description}
.card__actions
a(href="/admin/edit-product/:product.id:?edit=true") Edit
form(action="/admin/delete-product" method="POST")
.btn
button(class="btn", type="submit") Remove
else
h1 No Product
密切关注我在products.pug文件中的'a‘标签。我认为这就是问题的根源所在。格式可能会停止尝试遵循一个工具栏教程,并且它们可以在文件中设置查询参数,但是在pug中,我想它是不同的。
发布于 2022-02-14 19:37:48
下面是如何从锚标记中传回一个参数。
a( href='editticket/'+ticket.id ) ticket.id
这条路看起来像这样:
app.get('/editticket/:ticketId', (req,res, next)=>{
tktRoute.showTicket(req, res).catch(next);
});
showTicket路由检索该项并将其显示在窗体上。
https://stackoverflow.com/questions/71003713
复制相似问题