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

205
Visualizações
Best practices to handle Cart

I am trying to create a E-commerce project using MERN stack and was wondering. How to go about creating a cart for a user. I have currently user and products route and model set-up and have created a model for cart which has a id field referring back to the user.

My confusion is how to go about defining the routes for this cart in a RESTful way and how to go about linking the cart to the user.

  1. Should the route be like /carts/:id or should the route be declared inside the User routes and the route would be something like /users/me/cart ?

  2. If the user is signing up. How should the cart be linked to the user. Should it be created at the time user is getting created, like below. or how to go about creating it and linking it to the user. This is confusing to me since the route should be only defined for specific task and here we are using the /users route to also do cart creation.

    // Create user
    router.post('/users', async (req, res) => {
        const user = new User(req.body);
        const cart = new Cart({ owner: user._id });
        try {
            await user.save();
            await cart.save();
            const token = await user.generateAuthToken();
    
            res.status(201).send({ user, token, cart_id: cart._id });
        } catch (e) {
            res.status(500).send(e);
        }
    });
    

Also, What would be the best practices to handle this type of situation?

Any documentation or suggestions will be appreciated. Thanks for your help in advance!

over 4 years ago · Santiago Trujillo
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