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

273
Visualizações
Why I cannot send a string in send in express?

I have the following code to get data from an external API:

const axios = require('axios');
const express =  require('express');

const app = express()

app.get("/coins", (req, res) => {
    console.log("/coins test");

    axios.get("https://www.cryptingup.com/api/markets")
       .then(resultat=>{
         console.log(resultat.data.markets)
           res = resultat.data.markets
           myS="";

               res.forEach(element=>{
               console.log(element.exchange_id);
               myS=myS+element.exchange_id;
            });

            res.send(myS);
         
       })
       .catch(error => console.log(`error is ${error}`));
})

port = 3000

app.listen(port, ()=>{console.log(`The server has started and is listening on port number: ${port}`);});

when I execute the code I get the following:

error is TypeError: res.send is not a function

I dont know where is my error.

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

0

You've overwritten the value of res that was passed into the function with this line of code:

res = resultat.data.markets

So, after doing that the original res is gone so you can't call res.send() any more.

Define your own separately named variable:

app.get("/coins", (req, res) => {
    console.log("/coins test");

    axios.get("https://www.cryptingup.com/api/markets")
       .then(resultat => {
           console.log(resultat.data.markets);
           let markets = resultat.data.markets;
           let myS = "";

           markets.forEach(element => {
               console.log(element.exchange_id);
               myS = myS + element.exchange_id;
           });

           res.send(myS);
         
       })
       .catch(error => console.log(`error is ${error}`));
})

Also, you must declare all variables you use with let or const.


FYI, you could simplify a bit with:

let myS = resultat.data.markets.map(element => element.exchange_id).join("");
res.send(myS);
about 4 years ago · Juan Pablo Isaza Relatório

0

Your problem is that you are grabbing the "res" object which is passed into the express callback function, but then for whatever reason you reassign it to equal some results from your axios call, essentially removing all of the Express methods on "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