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

173
Visualizações
Sequelize "object" does not have increment function

I am using generate sequelize tables from Django generated models with sequelize-auto in my project. so far so good. I wrote the code to update the rank if I see a url again.

const findLink = async (link) =>
  Link.findOne({
    where: {
      url: link,
    },
    raw: true,
  });
// eslint-disable-next-line no-use-before-define
const insertEvent = async (link, tweetText) => {
  // Sync Database table before reading
  await sequelize.sync();
  findLink(link).then((url) => {
    if (url) {
      url.increment("rank", {by: 1}).then(()=>{
      Event.create({
        url_id: url.id,
        tweet_text: tweetText,
        created_at: new Date(),
        updated_at: new Date(),
      })
    });

    } else {
      Link.create({
        url: link,
        rank: 0,
        created_at: new Date(),
        updated_at: new Date(),
      }).then((newLink) => {
        Event.create({
          url_id: newLink.id,
          tweet_text: tweetText,
          created_at: new Date(),
          updated_at: new Date(),
        });
      });
    }
  });
};

But the problem is that when It execute url.increment("rank", {by: 1}) it says that url does not have increment function.

But according to documentation it is clearly stated here. Please let me know if I am doing something wrong? I have searched the internet but I could not find any thing relative. I can update the value with duplicate look up but I am looking for a way If I could update the already found object instead of searching it again.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are using raw queries

const findLink = async (link) =>
  Link.findOne({
    where: {
      url: link,
    },
    raw: true,
  });

It doesn't return an instance of the Model

See https://sequelize.org/master/manual/raw-queries.html

Edit:

By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows, etc).

A second option is the model. If you pass a model the returned data will be instances of that model.

// Callee is the model definition. This allows you to easily map a query to a predefined model
const projects = await sequelize.query('SELECT * FROM projects', {
  model: Projects,
  mapToModel: true // pass true here if you have any mapped fields
});
// Each element of `projects` is now an instance of Project

So the mapToModel option might also work

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