图像不会显示在显示页面上

时间:2016-10-31 10:25:58

标签: html node.js mongodb mongoose handlebars.js

我无法显示保存在数据库中的图像。在产品的展示页面上。在展示页面中,当我显示蛋糕图像时,网址被更改。我已将代码粘贴在下面。

这是route.js

var express = require('express');
var router = express.Router();

var Cupcake = require('../app/models/cupcakes');
var Cart = require('../app/models/cart');
var Order = require('../app/models/order')
/* GET home page. */
router.get('/', function(req, res, next) {
	var successMsg = req.flash('success')[0];
	Cupcake.find(function(err, cupcakes){
		if(!err){
  			res.render('index', {
  				title: 'Cupcakelicious',
  				cupcakes: cupcakes,
  				successMsg: successMsg,
  				noMessage: !successMsg
  			});
			// console.log(cupcakes);
		}else{
			return console.log(err);
		}
	});
});

router.get('/cupcake/:id', function(req, res, next) {
	var cupcake_id = req.param('id')
	console.log(typeof cupcake_id)
	console.log(cupcake_id)
	Cupcake.findOne({'_id': cupcake_id},function(err, cupcakes){
		if(!err){
			var cupcake =[];
			var c = cupcakes
			cupcake.push(c);
  			res.render('show', {
  				title: 'Cupcakelicious',
  				cupcake: cupcake
  			});
			// console.log("dfafsdsasdafdsagsd", cupcake);
		}else{
			return console.log(err);
		}
	});
});

module.exports = router;

这是带有图片网址的种子文件

var Cupcake = require('../models/cupcakes');
var mongoose = require('mongoose');

mongoose.connect('localhost:27017/salud')

var cupcakes = [
	new Cupcake({
		imageURL: 'Chocolate.jpg',
		Name: 'Chocolate cupcake',
		description: 'Freegan normcore vegan twee hell of. Trust fund vape edison bulb, health goth chartreuse pabst prism DIY tumeric distillery humblebrag normcore blue bottle coloring book.',
		price: 2.00
	}),
	new Cupcake({
		imageURL: 'vanilla.jpg',
		Name: 'Recess Peices cupcake',
		description: 'Freegan normcore vegan twee hell of. Trust fund vape edison bulb, health goth chartreuse pabst prism DIY tumeric distillery humblebrag normcore blue bottle coloring book.',
		price: 2.50
	})
];

var done = 0;
for (var i=0; i < cupcakes.length; i++){
	cupcakes[i].save(function(err, result){
		done++;
		if(done === cupcakes.length){
			exit();
		}
	});
}

function exit(){
	mongoose.disconnect();
}

这是一个杯形蛋糕正在展示而未被发现的地方

  <div class="row show-padding">
  <% for(var i=0; i < cupcake.length; i++){ %>
    <div class="center-div">
      <div class="col-md-4 col-md-offset-2">
        <a href="#" class="thumbnail">
          <img src="<%= cupcake[i].image%>" alt="..." class="img-responsive show-img">
        </a>
      </div>
      <div class="col-md-6 div-fonts">
        <h1><%= cupcake[i].Name %></h1><br>
        <h2>$<%= cupcake[i].price %></h2><br>
        <h4>Cupcake info: </h4>
        <p><%= cupcake[i].description%></p><br>
        <hr align="left" width="50%">
        <a href="/add-cart/<%= cupcake[i]._id%>" role="button" class="btn btn-success btn-lg">Add To Cart</a>
      </div>

    </div>
    <% } %>
  </div>

1 个答案:

答案 0 :(得分:1)

嗯,你正在如下保存纸杯蛋糕:

new Cupcake({
    imageURL: 'Chocolate.jpg',
    Name: 'Chocolate cupcake',
    description: 'Freegan normcore vegan twee hell of. Trust fund vape edison bulb, health goth chartreuse pabst prism DIY tumeric distillery humblebrag normcore blue bottle coloring book.',
    price: 2.00
}),

并访问它

cupcake[i].image

将其更改为:cupcake[i].imageURL应该可以解决问题,前提是它是正确的URL。我猜你需要调整路径,比如http://your.domain/whereimagesaresaved/Chokolate.jpg