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

250
Visualizações
How to call on a Prototype from inside a Hook in a Model?

I use Sequelize and in my User model have a hook and a prototype (see below). On the second line of the hook, I try to reference the prototype. But this doesn't work. It returns

this.getAllUsersInclMembersis not a function

How can I call upon the prototype from inside the hook?

module.exports = (sequelize) => {

    User.afterUpdate(async (options) => {
        // here I try to call upon the prototype for the instance
        let users = await this.getAllUsersInclMembers();
        await Promise.all(
            await users.forEach(async (user) => {
                // here we reference to a prototype in another model
                user.syncToDb();
            })
        );
    });

    User.prototype.getAllUsersInclMembers = function {
        return sequelize.models.Product.findAll({
            where: { user_id: this.getDataValue("id") },
        });
    };

};

Update: I've updated the hook to the code below but still with the same result:

User.afterUpdate(async function (user, options) {
    // here I try to call upon the prototype for the instance
    let users = await this.getAllUsersInclMembers();
    await Promise.all(
        await users.forEach(async (user) => {
            // here we reference to a prototype in another model
            user.syncToDb();
        })
    );
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've solved it:

User.addHook("afterUpdate", async function (instance, options) {
    let users = await instance.getAllUsersInclMembers();
    await Promise.all(
        await users.forEach(async (user) => {
            // here we reference to a prototype in another model
            user.syncToDb();
        })
    );
});
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