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

240
Visualizações
TypeError: Cannot read property 'get' of undefined - node.js - url params

I am trying to get certain params from my client-side get request (which come from user input), to feed into an API get a request I send off on my server.

I get this error when using .get on my search_params variable. I am using the 'URL' node.js package.

not sure why it is undefined I followed this guide to work out a solution to what I am trying to solve - 1

Code below: (I commented out the two lines that are causing the error so it is easier for you to see)

const axios = require('axios');
const router = require('express').Router();
const url = require('url');
require('dotenv').config();

const bamboo_api_key = process.env.BAMBOO_API_KEY;

const config = {
    headers: {
        'Accept':'application/json' 
    }
};

router.get('/bamboo/absencees', (req, res) => {

    const full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
    const search_params = full_url.searchParams;
    // const start = search_params.get('start')
    // const end = search_params.get('end')

    console.log("full url: ", fullUrl);
    console.log(end);
    console.log(sart);


    axios.get('https://'+bamboo_api_key+':x@api.bamboohr.com/api/gateway.php/velocitypartners/v1/time_off/whos_out/'+req.query.params, config)
    .then((response) => {
        console.log(response)
        console.log(req.query.params)
        res.send(response.data)
    })
    .catch(error => {
        console.log(error)
    });
})

module.exports = router;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

From what I see full_url is just a concatenated string which obviously has no method/attribute searchParams like an object would have.

So we know that full_url.searchParams is undefined. This would evaluate to

const search_params = undefined

which results in your problem.

Did you use a debugger? It would be helpful to see what full_url looks like during run-time. Nonetheless, it being a string should be the problem here.

about 4 years ago · Juan Pablo Isaza Relatório

0

There is a small thing, you need to know and add. full_url is a string that doesn't have searchParams. So, make it a URL object and use their methods.



const axios = require('axios');
const router = require('express').Router();
const url = require('url');
require('dotenv').config();

const bamboo_api_key = process.env.BAMBOO_API_KEY;

const config = {
    headers: {
        'Accept':'application/json' 
    }
};

router.get('/bamboo/absencees', (req, res) => {

    const full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
   const myURL = new URL(full_url); // make it an URL object to use its methods
    const search_params = myURL.searchParams;
    const start = search_params.get('start')
    const end = search_params.get('end')

    console.log("full url: ", fullUrl);
    console.log(end);
    console.log(sart);



// you can use string literal here
  axios.get(`https://${bamboo_api_key}:x@api.bamboohr.com/api/gateway.php/velocitypartners/v1/time_off/whos_out/${req.query.params}`  , config)
    .then((response) => {
        console.log(response)
        console.log(req.query.params)
        res.send(response.data)
    })
    .catch(error => {
        console.log(error)
    });
})

module.exports = router;
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