Variable not defined in EJS template

<input class="form-control" type="text" name="name" value="<%= %>" placeholder="name"> product is not defined

This is my route file:

const express = require('express')
const router = express.Router()
const imageMimeTypes = ['image/jpeg', 'image/png', 'images/gif']

//GET product model
var Product = require('../models/product')

//GET Category model
var Category = require('../models/category')

//GET All product Page
router.get('/', async(req, res) => {
    let query = Product.find()
    if ( != null && != '') {
        query = query.regex('name', new RegExp(, 'i'))
    try {
        const products = await query.exec()
        res.render('admin/products', {
            products: products,
            searchOptions: req.query
    } catch {

//GET category page
router.get('/add-product', async(req, res) => {

//POST Add page'/', async(req, res) => {

    const product = new Product({
        category: req.body.category,
        price: req.body.price,
        shopName: req.body.shopName,
        district: req.body.district,
        productCount: req.body.productCount,
        description: req.body.description
    saveCover(product, req.body.cover)

    try {
        const newProduct = await
            // res.redirect(`products/${}`)
    } catch {
        renderNewPage(res, products, true)

async function renderNewPage(res, products, hasError = false) {
    try {
        const categories = await Category.find({})
        const params = {
            categories: categories,
            product: product
        if (hasError) params.errorMessage = 'Error Adding Product'
        res.render('admin/add_product', params)
    } catch {

function saveCover(book, coverEncoded) {
    if (coverEncoded == null) return
    const cover = JSON.parse(coverEncoded)
    if (cover != null && imageMimeTypes.includes(cover.type)) {
        product.productImage = new Buffer.from(, 'base64')
        product.productImageType = cover.type

module.exports = router;
Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

sir this is my final project i want to upload product image with details to mongodb Atlas but this is not work for me can you help me

this is my ejs file add_product.ejs

<%- include (‘…/partials/adminheader’) %> <section class=“container”> <%- include (‘…/partials/messages’) %> <form action=“/admin/products” method=“post” enctype=“multipart/form-data”> <div class=“form-group”> <label for=“”>name</label> <input class=“form-control” type=“text” name=“name” value=“<%= %>” placeholder=“name”> </div> <div class=“form-group”> <label for=“”>description</label> <textarea class=“form-control” name=“description” rows=“5” cols=“20” placeholder=“description”><%= product.description %></textarea> </div> <div class=“form-group”> <label for=“”>Category</label> <select name=“category”> <% categories.forEach(category => { %> <% if ( === product.category) { %> <option selected label=“<%= category.slug %>” value=“<%= %>”> <% } else { %> <option label=“<%= category.slug %>” value=“<%= %>”> <% } %> <% }) %> </select> </div> <div class=“form-group”> <label for=“”>Price</label> <input class=“form-control” type=“text” name=“price” value=“<%= parseFloat(product.price).toFixed(2) %>” placeholder=“Price”> </div> <div class=“form-group”> <label for=“”>Location</label> <input class=“form-control” type=“text” name=“district” value=“<%= product.district %>” placeholder=“Price”> </div> <div class=“form-group”> <label for=“”>Shop Name</label> <input class=“form-control” type=“text” name=“shopName” value=“<%= product.shopName %>” placeholder=“Price”> </div> <div class=“form-group”> <label for=“”>Product Quantity</label> <input class=“form-control” type=“text” name=“productCount” value=“<%= product.productCount %>” placeholder=“Price”> </div> <div class=“form-group”> <label>Image</label> <input type=“file” name=“cover” class=“filepond”> </div> <button class=“btn btn-default”>submit </button> <div class=“cn fg”><a href=“/admin/categories”>back to all pages</a></div> </form> </section>

<%- include (‘…/partials/adminfooter’) %>

this is my model file product.js

const mongoose = require(‘mongoose’)

const productSchema = new mongoose.Schema({ name: { type: String, required: true }, description: { type: String }, productCount: { type: Number, required: true }, postedAt: { type: Date, required: true, default: }, productImage: { type: Buffer, required: true }, productImageType: { type: String, required: true }, category: { type: mongoose.Schema.Types.ObjectId, required: true, ref: ‘Category’ }, district: { type: String, required: true }, price: { type: String, required: true }, shopName: { type: String, required: true } })

productSchema.virtual(‘productImagePath’).get(function() { if (this.productImage != null && this.productImageType != null) { return data:${this.productImageType};charset=utf-8;base64,${this.productImage.toString('base64')} } })

module.exports = mongoose.model(‘Product’, productSchema)