Split image path gotten from database and append to src

Viewed 212

I have multiple image paths saved into the database in the same column. The image is saved in this sample in the database: enter image description here

Two images are not uploaded every time (the highlighted is the multiple image upload example separated by comma). I am using Nodejs and am getting the image data with foreach on the ejs side (data is the output of the select statement on the index.ejs side

index.js

router.get('/homes', function (req, res, next) {
  db.query("SELECT * FROM stocks", function (err, rs) {
    if (err) {
      console.log(err);
    }
    else {
      res.render('homes', {data:rs})
    }
  });
});
<% data.forEach(function(item) { %>
    <input type="hidden" id="desc" value="<%= item.image %>" />
    <img id="pic1" class="pic-1" src = "" alt="image"  >
<% }); %> 

item.image value (input string you are getting from $('#desc').val()). It is just string format of the image path as the loop occurs.

MyImage-1591506118979.png
MyImage-1591507932201.jpg
photos-1591548210637.jpg,photos-1591548210640.png
MyImage-1591494888039.png
MyImage-1591505437596.jpg
MyImage-1591084895899.jpg
MyImage-1591085173153.jpg
MyImage-1590905192772.JPG

I am saving the image on my localhost server. I am doing the split and wanting to append the src in the script tag with the code below:

<script>
  var desc = $('#desc').val();
  var temp = new Array();
  temp = desc.split(",");
  var container = document.getElementById("container");
  temp.forEach(function (imagepath) {
   var img = document.createElement("img");
   img.className = "class";
    img.src = imagepath;   
   // div.innerText = text;
    img.classList.add("li-added");
    container.append(img);
});

document.getElementById("pic1").src= "/" + text;

</script>

I use the same format for split text and it works, but am not sure what am getting wrong in the image part. I will appreciate any assistance or any other way to achieve this. Thanks.

1 Answers

I still don't know exactly what you intend to do. However, I tried to tidy up your code. In the HTML I got rid of the non-unique IDs by replacing them with class attributes.

In your JavaScript code I replaced the lengthy vanilla-JavaScript expressions by suitable jquery expressions and avoided some (temporary) variables altogether. The following is valid HTML and working JavaScript but might not be what you intended:

// var desc = $('#desc').val();
var data=[{image:"MyImage-1591506118979.png"},
{image:"MyImage-1591507932201.jpg"},
{image:"photos-1591548210637.jpg,photos-1591548210640.png"},
{image:"MyImage-1591494888039.png"},
{image:"MyImage-1591505437596.jpg"},
{image:"MyImage-1591084895899.jpg"},
{image:"MyImage-1591085173153.jpg"},
{image:"MyImage-1590905192772.JPG"}];

$("#container").html(data.map(item=>
    `<input type="text" class="desc" value="${item.image}" /><br>`
    + item.image.split(',').map(pth=>`<img class="pic1" src ="${pth}" alt="${pth}"><br>`)).join(''));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="container"></div>

Related