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

435
Visualizações
TypeError: callback is not a function at Timeout._onTimeout

I am trying to save a document in async mongodb schema validator in node.js. I set a custome validation 'tag' named attribute in mongodb schema. The validator in 'tag' is async. The program adds the document in the the mongodb but after the 4 seconds (setTimeout()), it gives the error that callback is not a function.

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/playground')
    .then(() => console.log('Connected with MongoDB...'))
    .catch(err => console.log("Couldn't connect with MongoDB!!!", err));

const courseSchema = new mongoose.Schema({
    name: { type: String, required: true, minlength: 3, maxlength: 55 },
    author: String,
    edition: Number,
    categories: {
        type: String,
        enum: ['web', 'mobile', 'research', 'networking'],
        required: true
    },
    tags: {
        type: Array,
        validate: {
            isAsync: true,
            validator: function (v, callback) {
                setTimeout(() => {
                    const result = v && v.length > 0;
                    callback(result);
                }, 4000);
            },
            message: 'A course should have at least one tags'
        }
    },
    data: { type: Date, default: Date.now },
    isPublished: Boolean,
    price: { type: Number, min: 5, max: 50, required: function () { return this.isPublished } }
});

const Course = mongoose.model('Course', courseSchema);

async function createCourse() {
    const course = new Course({
        name: "Compiler Constructions",
        author: "Mudassir",
        edition: 2,
        categories: 'web',
        tags: ['CUI'],
        isPublished: false,
        price: 8
    });
    try {
        const result = await course.save();
        console.log(result);
    }
    catch (err) {
        console.log(err.message);
    }
}

createCourse();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on docs and promise, try this:

  tags: {
    type: Array,
    validate: {
      validator: function (v) {
        return new Promise((resolve) => {
          setTimeout(() => {
            const result = v && v.length > 0;
            resolve(result);
          }, 4000);
        });
      },
      message: 'A course should have at least one tags',
    },
  },
about 4 years ago · Juan Pablo Isaza Relatório
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