This works for me on the route part of desired endpoint
var express = require("express");
var router = express.Router();
const grant = require("../controllers/grant");
const { body, check } = require("express-validator");
const {
authenticate,
checkIfAdmin,
checkIfSuper,
checkIfApplicant,
} = require("../middlewares/authentication");
const { validateGrant } = require("../middlewares/validator");
const { uploadHandler } = require("../middlewares/call");
const {
multerUpload,
cloudinaryConfig,
paystack,
} = require("../config/config");
uploadHandler = (req, res, next) => {
req.upload(req, res, (err) => {
if (err) {
res.status(400)
.json({ message: `Bad request upload handler, ${err.message}`, success: false })
.end();
} else {
// special workaround for files validating with express-validator
req.body.files = req.files;
next();
// console.log("req.files", req.upload, "here", req.files, "body", req.body.files);
}
});
};
router.post(
"/create",
authenticate,
checkIfSuper,
function (req, res, next) {
req.upload = multerUpload.fields([
{ name: "main", maxCount: 1 },
{ name: "banner", maxCount: 1 },
]);
next();
},
uploadHandler,
[
check("name").not().isEmpty().withMessage("Grant Name is required"),
check("description")
.isString()
.withMessage("Grant Description is required"),
check("sdgGoals")
.not()
.isEmpty()
.withMessage("Grant Sdg Goals are required"),
check("deadline")
.not()
.isEmpty()
.withMessage("Grant Application Deadline is required"),
check("available")
.isBoolean()
.withMessage("Grant Availability is required"),
check("about").isString().withMessage("Grant About is required"),
check("by").isString().withMessage("Grant Creator is required"),
],
cloudinaryConfig,
grant.create
);
grant.create is
const { error } = require("../middlewares/response");
const { grants } = require("../models/grant");
const { validationResult } = require("express-validator");
const Datauri = require("datauri/parser");
const path = require("path");
const { uploader } = require("../config/config");
exports.create = async function (req, res, next) {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
success: false,
message: errors.array()[0].msg,
});
}
if (!req.files) {
return res.status(400).json({
success: false,
message: "No files were uploaded",
});
}
const dUri = new Datauri();
const dataUri = (file) =>
dUri.format(path.extname(file.originalname).toString(), file.buffer);
const {
title,
description,
sdgGoals,
applicationDeadline,
applicationLimit,
available,
} = req.body;
let promises = [];
let imgs = [];
// console.log("req.files", req.files, "here", req.body.files);
for (const key in req.files) {
if (Object.hasOwnProperty.call(req.files, key)) {
const f = req.files[key][0];
console.log(f, "before uri")
//console.log(dataUri(f)); -> //fileName: '.png', mimetype: 'image/png', and content
const file = dataUri(f).content;
try {
const resp = await uploader.upload(file);
// console.log("resp url and image", resp.url, image);
imgs.push({ name: key, url: resp.url });
} catch (err) {
return next(err);
}
}
}
console.log("imgs", imgs);
const grant = new Grant({
...req.body,
createdBy: req.user._id,
images: imgs,
});
const result = await grant.save();
return res.status(201).json({
message: "Grant created successfully",
data: {
grant: result,
},
});
} catch (err) {
next(err);
}
};