2016-10-31 72 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の代わりにmongooseのスキーマや文字列などの情報を入力してください。 –

答えて

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

+0

ありがとうございました:) –

関連する問題