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

202
Visualizações
How to increase click count value when visiting an URL

I am building an URL shortener. So I want to track datewise click count when a URL is visited using the short URL. For example: on 24th January if the URL was visited by using the short URL it will show the click count. I want the data to look like this -

dateWiseCount: [
      {
        date: '01/24/2022',
        clickCount: 5,
      },
      {
        date: '01/25/2022',
        clickCount: 8,
      },
]

I have been able to get the total number of clicks when a URL is visited by using the short URL but for some reason whenever I hit the short URL it is registering date wise click Count like this -

dateWiseCount: [
      {
        date: '01/24/2022',
        clickCount: 0,
      },
      {
        date: '01/24/2022',
        clickCount: 0,
      },
]

So it is not increasing the click count and also creating a new array object with the same date. This is what I am getting from MongoDB -

"totalClicks": 2,
    "dayWiseClicks": [{
        "dailyClicks": 0,
        "_id": {
            "$oid": "61ee56f42b85726d004d535d"
        },
        "date": {
            "$date": "2022-01-24T07:36:20.429Z"
        }
    }, {
        "dailyClicks": 0,
        "_id": {
            "$oid": "61ee57172b85726d004d5361"
        },
        "date": {
            "$date": "2022-01-24T07:36:55.684Z"
        }
    }],

This is my code :

exports.redirect = async (req, res) => {
  const url_hash = req.params.shortUrl;

  await URL.findOne({ url_hash })
    .then((url) => {
      const condition = { _id: url.id };
      let counter = 0;
      const dayWiseNewClicks = {
        date: new Date(),
        dailyClicks: counter++,
      };
      const update = { $push: { dayWiseClicks: dayWiseNewClicks } };
      if (url) {
        url.totalClicks++;
        URL.updateOne(condition, update)
          .then(() => {
            console.log("Push Succesful");
          })
          .catch((error) => {
            return res.status(404).json({ updateError: error.message });
          });
        url.save();
        return res.redirect(url.original_url);
      } else {
        return res.status(404).json({ error: "No Url Found" });
      }
    })
    .catch((error) => {
      return res.status(404).json({ error: error.message });
    });
};

How Can I solve this problem? Please help me out. Stuck on this for a few hours now.

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