<input class="form-control" type="text" name="name" value="<%= product.name %>" 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 (req.query.name != null && req.query.name != '') {
        query = query.regex('name', new RegExp(req.query.name, 'i'))
    }
    try {
        const products = await query.exec()
        res.render('admin/products', {
            products: products,
            searchOptions: req.query
        })
    } catch {
        res.redirect('/')
    }
});

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

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

    const product = new Product({
        name: req.body.name,
        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 product.save()
            // res.redirect(`products/${newProduct.id}`)
        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 {
        res.redirect('/products')
    }
}

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(cover.data, 'base64')
        product.productImageType = cover.type
    }
}


module.exports = router;
edited by MattIPv4
1 comment
  • @sasisp2000 Can you provide some more context here? On what page is this error occurring? What code surrounds the line that is erroring? What does your product model look like (what does the find method return)?

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.

×
2 answers

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=“<%= product.name %>” 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 (category.id === product.category) { %>
<option selected label=“<%= category.slug %>” value=“<%= category.id %>”>
<% } else { %>
<option label=“<%= category.slug %>” value=“<%= category.id %>”>
<% } %>
<% }) %>
</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: Date.now
},
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)

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

Submit an Answer