如何在ExpressJS的循环中创建内部循环?

时间:2018-12-28 12:56:58

标签: express ejs

这是在http://localhost:3000/处呈现的html视图:

43038

防火

{https://ae01.alicdn.com/kf/HTB1AhTgQpXXXXb9apXXq6xXFXXXO/-1-PCS-12VDC-Wired-Cable-Link-indoor-Smoke-Detector-Fire-Alarm-sensor-personal-Home-security.jpg,https://ae01.alicdn.com/kf/HTB1U_rPQpXXXXb4XpXXq6xXFXXXS/-1-PCS-12VDC-Wired-Cable-Link-indoor-Smoke-Detector-Fire-Alarm-sensor-personal-Home-security.jpg,https://ae01.alicdn.com/kf/HTB1w_K5EKuSBuNjSsziq6zq8pXaH/-1-PCS-12VDC-Wired-Cable-Link-indoor-Smoke-Detector-Fire-Alarm-sensor-personal-Home-security.jpg,https://ae01.alicdn.com/kf/HTB1bTCEEQ9WBuNjSspeq6yz5VXaz/-1-PCS-12VDC-Wired-Cable-Link-indoor-Smoke-Detector-Fire-Alarm-sensor-personal-Home-security.jpg,https://ae01.alicdn.com/kf/HTB1EMmlEKuSBuNjSsplq6ze8pXaJ/-1-PCS-12VDC-Wired-Cable-Link-indoor-Smoke-Detector-Fire-Alarm-sensor-personal-Home-security.jpg,https://ae01.alicdn.com/kf/HTB1qjP3ENWYBuNjy1zkq6xGGpXaf/-1-PCS-12VDC-Wired-Cable-Link-indoor-Smoke-Detector-Fire-Alarm-sensor-personal-Home-security.jpg}

这是/queries.js:

function getList(req,res,next){
    db.any("select * from products limit 10")
    .then(function(data){

        res.locals.data = {
            data:data
        };
        next();
    })
    .catch(function(err){
        return next(err);
    })
}

这是routes / index.ejs:

router.get('/',[db.getList], function(req,res){
         let data =  res.locals.data;
        res.render('getall',{result:data});
})

这是views / getall.ejs:

<% result.data.forEach(function(value){ %>
    <p> <%- result.data[0].id %></p>
    <p> <%- result.data[0].category %></p>
    <p><%= result.data[0].galleryimages %></p>
   <% }) %>

如您所见,“ id”和“ category”已正确呈现,但“ galleryimages”仍呈现为JSON。

如何在views / getall.ejs的原始/第一个循环中为此JSON创建循环,以便可以在<img>标签中将它们作为URL输入?

我尝试过:

<% for(var i = 0; i < result.data[0].galleryimages.length; i++) {%>
        <img src="<%= result.data[0].galleryimages[i] %>"/>
        <% } %>

但是它将每个URL分解为单独的字符作为单独的URL。

This how it looks like in the browser

0 个答案:

没有答案