Node.js 显示库图像时无法读取null的每个属性
这是我的产品方法,用于在主页上显示与其类别相关的所有产品Node.js 显示库图像时无法读取null的每个属性,node.js,express,sequelize.js,Node.js,Express,Sequelize.js,这是我的产品方法,用于在主页上显示与其类别相关的所有产品 //GET Products DEtails router.get('/:category/:product',function(req,res){ var galleryImages=null; models.Product.findOne({ where:{slug:req.params.product} }) .then(function(product){ var galleryDir='pub
//GET Products DEtails
router.get('/:category/:product',function(req,res){
var galleryImages=null;
models.Product.findOne({
where:{slug:req.params.product}
})
.then(function(product){
var galleryDir='public/product_images/' + product.id + '/gallery';
fs.readdir(galleryDir,function(files){
galleryImages=files;
res.render('product',{
title:product.title,
p:product,
galleryImages:galleryImages,
})
})
})
});
这是用于显示它的mine Product.ejs文件。上面的p.Images正在工作,但是forEach循环不工作。无法读取属性forEach的Null。mine Gallery Images的路径正确
<% include layouts/header %>
<div class="row">
<h1 class="page-header"><%= p.title %></h1>
<div class="col-xs-12 col-md-5">
<img class="spi" src="/product_images/<%= p.id %>/<%= p.images %>" alt="">
<br>
</div>
<div class="col-xs-12 col-md-7">
<p><%= p.desc %></p>
<p>$<%= parseFloat(p.price).toFixed(2) %></p>
<p><a href="/cart/add/<%= p.slug %>">Add To Cart</a></p>
</div>
<div class="col-xs-12">
<ul class="gallery">
<% galleryImages.forEach(function(images){ %>
<% if (images != "thumbs") { %>
<li>
<!--<a data-fancybox="gallery" href="/product_images/<%= p.id %>/gallery/<%= images %>"> -->
<img src="/product_images/<%= p.id %>/gallery/thumbs/<%= images %>" alt="">
</a>
</li>
<% } %>
<% }); %>
</ul>
</div>
</div>
<!--
- will gave Content Where as = will gave Html <P>tag in the Browser</P> -->
<% include layouts/footer %>
/“alt=”“>
$
-
这是我的产品.js试用后,效果不错
/*
* GET product details by category
*/
router.get('/:category/:product', function (req,res){
Product.findOne({slug: req.params.product}, function (err, product) {
if(!product) { //if product not exist in db
return res.status(404).send('Product not found');
}else{
var galleryDir = 'public/product_images/' + product._id + '/gallery';
fs.readdir(galleryDir, function (err, files) {
if (err) {
console.log(err);
} else {
galleryImages = files;
res.render('product', {
title: product.title,
p: product,
galleryImages: galleryImages
});
}
});
}
});
});
请把
files
的输出从galleryImages=files;
。在哪一行bro!!where galleryImages:galleryImages我试图将其更改为文件,但在这之后它就不工作了fs.readdir(galleryDir,函数(files){
我不懂你的意思,兄弟,当我有res.json时,文件的输出将变为空(档案)兄弟!!