<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;
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!
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=“<%= 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)
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.