2016-10-31 56 views
0

我无法显示保存在数据库中的图像。进入产品的展示页面。在显示页面时,我显示一个蛋糕图像的URL被更改。我在下面粘贴了代码。图片将不会显示在显示页面

这是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) { 
 
\t var successMsg = req.flash('success')[0]; 
 
\t Cupcake.find(function(err, cupcakes){ 
 
\t \t if(!err){ 
 
    \t \t \t res.render('index', { 
 
    \t \t \t \t title: 'Cupcakelicious', 
 
    \t \t \t \t cupcakes: cupcakes, 
 
    \t \t \t \t successMsg: successMsg, 
 
    \t \t \t \t noMessage: !successMsg 
 
    \t \t \t }); 
 
\t \t \t // console.log(cupcakes); 
 
\t \t }else{ 
 
\t \t \t return console.log(err); 
 
\t \t } 
 
\t }); 
 
}); 
 

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

 
module.exports = router;

这是与图像URL

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

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

 
var cupcakes = [ 
 
\t new Cupcake({ 
 
\t \t imageURL: 'Chocolate.jpg', 
 
\t \t Name: 'Chocolate cupcake', 
 
\t \t 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.', 
 
\t \t price: 2.00 
 
\t }), 
 
\t new Cupcake({ 
 
\t \t imageURL: 'vanilla.jpg', 
 
\t \t Name: 'Recess Peices cupcake', 
 
\t \t 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.', 
 
\t \t price: 2.50 
 
\t }) 
 
]; 
 

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

 
function exit(){ 
 
\t 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>

+0

请提供更多的信息,如猫鼬模式和你看到的字符串,而不是imageURL。 –

回答

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应该做的伎俩,只要它是正确的网址。我猜你需要调整路径艰难,像http://your.domain/whereimagesaresaved/Chokolate.jpg

+0

谢谢你工作:) –