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

119
Visualizações
Parameters not being passed to server router

I have this function where I pass some parameters to my server:

async function createOrderItemForCardInList(cardID, value, listID) {
    const token = await getToken();
    const response = await fetch(`${basePath}/order/list/${listID}`, {
        method: 'POST',
        body: JSON.stringify({
            cardID,
            value
        }),
        headers: getTokenAuthHeaders(token)
    });

    return response.json();
}

And on my server, I have the route defined for when the call is passed:

router.post('/list/:id', TokenController.isAValidToken, async (req, res) => {
    const { cardID, value } = req.query;
    console.log(value, cardID); // <- This prints 'undefined'

    try {
        const orderItem = await OrderItemController.createOrderItem(value, req.params.id, cardID);
        res.json(orderItem);
    } catch (error) {
        ErrorController.errorCallback(error, res);
    }
});

As you can see by the commentary on my router, the parameters of que request are not being passed, how can I resolve this?

I know for a fact that the parameters print their correspondent values when I place a console.log(cardID, value, listID) on the createOrderItemForCardInList function.

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

0

You are destructing a query which are added after a ? in a request. IE .com?cardID=777&value=10000

However in your original code you send it via the POST body. Change req.query to req.body to achieve the intended result.

router.post('/list/:id', TokenController.isAValidToken, async (req, res) => {
    const { cardID, value } = req.body;
    console.log(value, cardID); // <- This prints 'undefined'

    try {
        const orderItem = await OrderItemController.createOrderItem(value, req.params.id, cardID);
        res.json(orderItem);
    } catch (error) {
        ErrorController.errorCallback(error, res);
    }
});
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