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

88
Visualizações
MongoDB struggling to update DB

I'm new to MongoDB. I have a DB where cards data will be stored and update counts if the user likes the cards. Giving me a total likes number.

I keep getting my res as a 400 and I can't work out why. I got the update method to work manually. but when I put it into a function it does not work.

I'm using NEXT JS API routes.

import dbConnect from "../../utils/dbConnect";
import FavSchema from "../../models/FavoriteModel";

dbConnect();

export default async (req, res) => {
  const { method } = req;

  switch (method) {
    case "GET":
      try {
      } catch (error) {
        res.status(400).json({ success: false });
      }
      break;
    case "POST":
      try {
        const query = {
          cardId: req.body.cardHandler,
        };

        await FavSchema.findOneAndUpdate(
          query,
          { $inc: { favCount: 1 } },
          { upsert: true }
        );
        res.send("Succesfully saved.");
      } catch (error) {
        res.send(400, { error: "Error" });
      }
  }
};

function

 const handleCardFavClick = (id) => {
    const cardHandler = {
      id,
    };
    fetch("/api/favcounter", {
      method: "POST",
      body: JSON.stringify(cardHandler),
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((data) => console.log(data));
    dispatch(makeFav(cardHandler));
  };

Thank you!

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

0

You are passing

{ id: "<id>" } 

as body in your API and reading "cardHandler" from req.body, which is "undefined".

In your POST API, replace

const query = {
      cardId: req.body.cardHandler,
    }

with

const query = {
      cardId: req.body.id
    }
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