Click here to Skip to main content
15,298,916 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In shop.js controller file I rendered the product-details.ejs file and in that file I dynamically trying to retrieve the product.title description etc. when I try to execute it is throwing an error in product-detail.ejs file as title is not defined


shop.js controller

const Product=require('../models/product.js');


exports.getProducts=(req,res,next)=>{
    Product.fetchAll((products)=>{
        res.render('shop/product-list.ejs',{
            prods:products,
            pageTitle:'shop',
            path:'/'
        });

    });

};

exports.getProduct=(req,res,next)=>{
    const prodId=req.params.productId;
    Product.findById(prodId,product=>{
        res.render('shop/product-detail.ejs',{
            product:product,
            pageTitle: product.title,
            path:'/products'
        });
    });
};


exports.getIndex=(req,res,next)=>{
    Product.fetchAll((products)=>{
        res.render('shop/index.ejs',{
            prods:products,
            pageTitle:'index',
            path:'/'
        });

    });

};


exports.getCart=(req,res,next)=>{
    Product.fetchAll((products)=>{
        res.render('shop/cart.ejs',{
            prods:products,
            pageTitle:'cart',
            path:'/cart'
        });

    });

};

exports.getOrders=(req,res,next)=>{
    Product.fetchAll((products)=>{
        res.render('shop/order.ejs',{
            prods:products,
            pageTitle:'order',
            path:'/order'
        });

    });

};

exports.getCheckout=(req,res,next)=>{
    Product.fetchAll((products)=>{
        res.render('shop/checkout.ejs',{
            prods:products,
            pageTitle:'checkout',
            path:'/checkout'
        });

    });

};


template file product-detail.ejs

<%- include('../includes/head.ejs') %>
    </head>

    <body>
        <%- include('../includes/navigation.ejs') %>
        <main class="centered">
            <h1><%= product.title %></h1>
            <hr>
            <div>
                <img src="<%= product.imageUrl %>" alt="<%= product.title %>">
            </div>
            <h2><%= product.price %></h2>
            <p><%= product.description %></p>
            <form action="/cart" method="post">
                <button class="btn" type="submit">Add to Cart</button>
            </form>
        </main>
        <%- include('../includes/end.ejs') %>


What I have tried:

In shop.js controller file I rendered the product-details.ejs file and in that file I dynamically trying to retrieve the product.title description etc. when I try to execute it is throwing an error in product-detail.ejs file as title is not defined
Posted
Updated 12-May-20 21:33pm
Comments
Richard MacCutchan 9-Dec-19 4:37am
   
The message is actually telling you that object reference product is not defined.
pragspri 9-Dec-19 12:37pm
   
I defined product object in shop.js file exports.getProduct in the controller while rendering

1 solution

change the code in your models/product.js

 static findById(id, cb) {
   getProductsFromFile(products => {
     const product = products.find(p => p.id === id);
     cb(product === undefined ? "Product details" : product.title, product);
   });
}


by replacing
cb(product)

with
cb(product === undefined ? "Product details" : product.title, product);


then adjust your shop.js controller by changing
exports.getProduct=(req,res,next)=>{
    const prodId=req.params.productId;
    Product.findById(prodId,product=>{
        res.render('shop/product-detail.ejs',{
            product:product,
            pageTitle: product.title,
            path:'/products'
        });
    });
};

to
exports.getProduct = (req, res, next) => {
  const prodId = req.params.productId;
  Product.findById(prodId, (title,product) => {
     res.render('shop/product-detail', {
      product: product,
      pageTitle: title,
      path: '/products'
    });
  });
};
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900