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

193
Visualizações
how pass a var in the axios?

I'm trying to pass a var to the axios more by what I'm seeing it just accepts string is there any way to pass a var ?

My code:

router.get('/albuns', async (req, res)=>{
  const response = await axios.get(req.query.id)

  return console.log(response)
})

Error:

Argument of type 'string | ParsedQs | string[] | ParsedQs[] | undefined' is not assignable to parameter of type 'string'.

Type 'undefined' is not assignable to type 'string'.

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

0

This is just a Typescript warning because axios.get() expects a string argument but the default req.query can provide various types.

You just need to cast the value

const response = await axios.get(req.query.id as string);

Another option is to type the request in your handler signature

import { Request } from "express";

interface AlbunsQuery {
  id: string;
}

type AlbunsRequest = Request<{}, any, any, AlbunsQuery>;

router.get("/albuns", async (req: AlbunsRequest, res) => {
  const response = await axios.get(req.query.id);

  // etc
});
about 4 years ago · Juan Pablo Isaza Relatório

0

URL parameters aren't supported, and it seems they have no intention on adding it, given they expect you to use template strings. Here there's a discussion: https://github.com/axios/axios/issues/706

Axios GET method takes two arguments: the first being the URL string, and the second argument, we have it configure object with the property called params.

Axios Code:

axios.get("http://localhost/example"){
       params{
          name: myName
          example: 2
}}

Express Code:

app.get("example", (req, res){
const name : request.query.name
const example: request.query.example
}
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