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

231
Visualizações
How do I pass multiple search parameters in node js URL

I'm using react, node, express, postgress. How would I go about using express routes to pass multiple parameters? for example:

This is my route:

//Get entries from materials2 table
app.get("/materials2/:id1&:id2", async (req, res) => {
    try {

        const {material_thickness, material_width} = req.params;
        const contact = await pool.query(
            `SELECT *
            FROM materials_inventory
            WHERE
                (material_thickness = $1) AND
                (material_width BETWEEN ($2-0.5) AND ($2+0.5))`, [material_thickness, material_width]); 


        res.json(contact.rows[0]);

    } catch (err) {
        console.error(err.message);
    }
})

The idea is that I need to pass two parameters to the search query

From the react side I have:

const stateSetter = async (thickness, width, length, length2) => {
        try {
            
            const response = await fetch(`http://localhost:5000/materials/${thickness, width}`, [thickness, width])
            const jsonData = await response.json();

            console.log(thickness);

        } catch (err) {
            console.log(err.message)
        }
    }

So the idea is that stateSetter receives some values, uses those with the route. How can I go about this? Thanks!

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

0

You can use request query. Change the route in node like so:

//Get entries from materials2 table
app.get("/materials2", async (req, res) => {
    try {

        const {materialThickness, materialWidth} = req.query;
        const contact = await pool.query(
            `SELECT *
            FROM materials_inventory
            WHERE
            (material_thickness = $1) AND
            (material_width BETWEEN ($2-0.5) AND ($2+0.5))`, [materialThickness, materialWidth]); 


        res.json(contact.rows[0]);

    } catch (err) {
        console.error(err.message);
    }
})

And you call in the frontend like so:

const stateSetter = async (thickness, width, length, length2) => {
    try {
        
        const response = await fetch(`http://localhost:5000/materials2?materialThickness=${thickness}&materialWidth=${width}`)
        const jsonData = await response.json();

        console.log(thickness);

    } catch (err) {
        console.log(err.message)
    }
}

Also, you were calling the endpoint materials form you frontend, but specified it as materials2 in node. I updated that in the answer but keep in mind.

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