0

Getting null prototype error when uploading images.

Multer save images, but when I save images in the MongoDB then mongoose gives null prototype error:

imgUrl: {thumbnail: req.files.filename,gallery: req.files.filename,}

Please help me, as soon as possible.

Error Code:[Object: null prototype] {title: 'Arthar', description: 'the' } undefined undefined

JS Code:

    const express = require("express");
    const mongoose = require("mongoose");
    const bodyParser = require("body-parser");
    const path = require("path");
    const multer = require("multer");
    const {
      uuid
    } = require("uuidv4");

    const router = express.Router();
    mongoose.connect("mongodb://localhost/user", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });

    const db = mongoose.connection;
    db.on("error", console.error.bind(console, "connection error:"));
    db.once("open", function () {
      console.log("we are connected");
    });

    //body parser
    router.use(
      bodyParser.urlencoded({
        extended: true,
      })
    );
    router.use(bodyParser.json());

    //mongoose schema
    const gallerySchema = new mongoose.Schema({
      imgUrl: {
        thumbnail: String,
        gallery: Array,
      },
      title: String,
      description: String,
      date: {
        type: Date,
        default: Date.now,
      },
    });
    const gallery = mongoose.model("gallery", gallerySchema);

    router.get("/", (req, res) => {
      res.render("gallery");
    });

    //save uploaded files using multer
    let storage = multer.diskStorage({
      destination: (req, file, cb) => {
        cb(null, "../public/gallery/");
      },
      filename: (req, file, cb) => {
        let d = new Date();
        const imgName =
          file.fieldname + "_" + uuid() + path.extname(file.originalname);
        cb(null, imgName);
      },
    });
    let upload = multer({
      storage: storage,
    });

    let cpUpload = upload.fields([{
        name: "thumbnail",
        maxCount: 1,
      },
      {
        name: "gallery",
        maxCount: 20,
      },
    ]);

    router.post("/", cpUpload, (req, res) => {
      let a = new gallery({
        imgUrl: {
          thumbnail: req.files.filename,
          gallery: req.files.filename,
        },
        title: req.body.title,
        description: req.body.description,
      });
      a.save((err, a) => {
        if (err) return console.error(err);
        else {
          res.render("gallery");
        }
      });
      console.log(req.body);
      console.log(req.files.filename);
      console.log(req.files.filename);
    });
    module.exports = router;


Error Image

Akhilesh Mishra
  • 5,876
  • 3
  • 16
  • 32
  • Hey @Amit, I'm not sure about `Multer` never used but, the destination you gave is literal path and it may not find the path like that. maybe just try it with `path.resolve`? I'm just trying to look another angle, as i said i don't know about `Multer`; – halilcakar Oct 03 '20 at 02:10
  • what datatype you are using in model for saving imgUrl ?? it have to be like this you can check in this thread https://stackoverflow.com/a/42173267/12761193 – Aryan Oct 03 '20 at 04:26

0 Answers0