Creating a create / return controller method /API with image upload - PG, Node, Multer

I am trying to create an API for adding and returning details and an image into / from Postgres from Node.

I currently have the below controller API for both of these:

exports.createDiveSpot = (req, res) => {
    // new dive spot to be added to db


        diveLocation: req.body.diveLocation,
        diveRegionID: req.body.diveRegionID,
        diveSpotTypeID: req.body.diveSpotTypeID,
        diveSpotDescription: req.body.diveSpotDescription,
        diveSpotPhotos: fs.readFileSync(
            __basedir + "/resources/static/assets/uploads/" + req.file.filename
        .then((image) => {
                __basedir + "/resources/static/assets/tmp/" +,
            return res.send(`File has been uploaded.`);
        .then((diveSpot) => {
                statusCode: 200,
                success: true,
                message: "Success",
                diveSpot: diveSpot,
        .catch((err) => {
            res.status(500).send({ message: err.message });

exports.allDiveSpots = (req, res) => {

        .then((diveSpot) => {

            const diveSpotList = [];
            for (i = 0; i < diveSpot.length; i++) {

            if (!diveSpot) {
                return res.status(404).send({ message: "No dive spots are stored" });

                data: diveSpotList,
        .catch((err) => {
            res.status(500).send({ message: err.message });

routes'/api/divespot/createdivespot',upload.single("file"), controller.createDiveSpot);

 // return list of dive spots
 app.get('/api/divespot/alldivespots', controller.allDiveSpots);

I have the code for using multer middleware included as below:

const multer = require("multer");

// config for multer to disk storage engine
const imageFilter = (req, file, cb) => {
    if (file.mimetype.startsWith("image")) {
        cb(null, true);
    } else {
        cb("Please upload only images.", false);

//sustscub timestamp prefix to stop duplicates
var storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, __basedir + "/resources/static/assets/uploads/");
    filename: (req, file, cb) => {
        cb(null, `${}-SustScub-${file.originalname}`);

// filter only allows images to pass
var uploadFile = multer({ storage: storage, fileFilter: imageFilter });
module.exports = uploadFile;

My pg pgadmin database and model are as below:

I have been using Postman to try and Post this to my database but it is only posting text fields and I have been using the form_data field as shown in tutorials.

Is there anything obvious that I am doing wrong? The error message I’m getting on Postman suggests multer is causing problems but I’m not sure if the error is getting to the image being upload or if the text is causing the error. It was previously letting me upload the text in a json file to the database before I added the fileSync code.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.