I have successfully created a file to upload images, however the image name appears as undefined.jpg in this project. I am using the Express file upload middleware.
*admin.js
var express = require("express");
const productHelpers = require("../helpers/product-helpers");
var router = express.Router();
var productHelper = require("../helpers/product-helpers");
/* GET users listing. */
router.get("/", function (req, res, next) {
productHelpers.getAllProducts().then((products) => {
console.log(products);
res.render("admin/view-products", { admin: true, products });
});
});
router.get("/add-product", function (req, res) {
res.render("admin/add-product");
});
router.post("/add-product", (req, res, next) => {
productHelpers.addProduct(req.body, (id) => {
let image = req.files.Image;
console.log(id);
image.mv("./public/product-images/" + id + ".jpg", (err) => {
if (!err) {
res.render("admin/add-product");
} else {
console.log(err);
}
});
});
});
module.exports = router;
product-helpers.js
Here is where I do callback using id
product-helpers.js` var db=require('../config/connection')
var collection=require('../config/collections')
module.exports={
addProduct:(product,callback)=>{
db.get().collection('product').insertOne(product).then((data)=>{callback(data._id)})
}
}
`