简体   繁体   中英

how to pass image in to html img tag, nodejs mongoose express handlebars

Will try to explain and will be thankful for any help.

I write web on node.js with express and handlebars, I'm stacked on how to pass image to slider img tag ? Slider is on every page so i cannot pass image on every route, Image is stored in mongodb database, and image path is stored in img property.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ImageSchema = new Schema({
  img: {
    type: String,
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});
mongoose.model('image', ImageSchema);

nothing special,

my main layout is like this

<body>
{{> _navbar}} {{> _slider}} {{> _navbar2}}
<div class="container">
{{> _msg}} {{{body}}} {{> _errors}}
</div>
{{> _floatingbutton}} {{> _footer}}
</body>

How to pass image storage location to html ? or is there any other way how to assign images to slider ? i tried to write helper function for handlebar but i get only promise and handlebar does not accept promise... SOS !

Thnx for your great support people :) ... i have resolved a promise and passed string to img url but somehow it deleted backslashes and path was not correct. here is how i fixed it

i created another js file and put in script on body

function slider() {
let sliderImage = document.getElementById('slide');
let url = sliderImage.style.backgroundImage.split(`url("/imagesgalleryimg`)
urlEnds = url[1].split(`")`);
// let sliderImage = document.getElementById('slide');
// sliderImage.style.backgroundImage = "none"

sliderImage.style.backgroundImage = `url(/images/gallery/img${urlEnds[0]})`
console.log(sliderImage)
 }

slider();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM