Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
TypeError: Image is not a constructor const newImg = new Image({

when I try to upload an image and save its title, description and filename in the database, I get this error, please help, I am using NodeJS and mongoose. please help to solve this problem and, if possible, a brief explanation of the problem.

const newImg = new Image({
                ^

TypeError: Image is not a constructor

This is the code

const path = require('path');
const { randomNumber } = require('../helpers/libs.js');
const fs = require('fs-extra');


const ctrl = {};

const { Image, Comment } = require('../models/');

ctrl.index = async (req, res) => {
    let viewModel = { image: {}, comments: [] };
    const image = await Image.findOne({filename: { $regex: req.params.image_id }});
    if (image) {
      image.views = image.views + 1;
      viewModel.image = image;
      image.save();
      const comments = await Comment.find({image_id: image._id})
        .sort({'timestamp': 1});
      viewModel.comments = comments;
      viewModel = await sidebar(viewModel);
      res.render('image', viewModel);
    } else {
      res.redirect('/');
    }
};

ctrl.create = async (req, res) => {
    const imgUrl = randomNumber();
    console.log(imgUrl);
    const imageTempPath = req.file.path;
    const ext = path.extname(req.file.originalname).toLowerCase();
    const targetPath = path.resolve(`src/public/upload/${ imgUrl }${ ext }`); 
    
    if (ext === ".png" || ext === ".jpg" || ext === ".jpeg" || ext === ".gif") {
        await fs.rename(imageTempPath, targetPath);
        const newImg = new Image({
            title: req.body.title,
            description: req.body.description,
            filename: imgUrl + ext,
        });
        const imageSave = await newImg.save();
    }
    res.send('recibido');

};

Here is Schema

const mongoose = require('mongoose');
const { Schema, model } = mongoose;
const path = require('path');


const ImageSchema = new Schema({
  title: { type: String },
  description: { type: String },
  filename: { type: String },
  views: { type: Number, default: 0 },
  likes: { type: Number, default: 0 },
  timestamp: { type: Date, default: Date.now }
});


ImageSchema.virtual('uniqueId')
  .get(function () {
    return this.filename.replace(path.extname(this.filename), '');
  });

module.exports = mongoose.model('Image', ImageSchema);

Please Help

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!