Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
TypeError: newImg.save is not a function

I am new to this world of programming, I am currently with my first project, but I have an error, please help, also if possible a brief explanation of why the problem is and how to avoid it in future projects. (I using NodeJs and Mongoose)

ERROR:

TypeError: newImg.save is not a function

CODE:

    const path = require('path');
    const { randomNumber } = require('../helpers/libs.js');
    const fs = require('fs-extra');
    const Imagen = function(){
    
    };
    
    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 Imagen({
                title: req.body.title,
                description: req.body.description,
                filename: imgUrl + ext,
            });
            const imageSave = await newImg.save();
        }
        res.send('recibido');
    
    };

this 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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda