2016-10-31 86 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

謝謝你工作:) –