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

236
Visualizações
Can you get node js to send an array not a string

I want the code to return a 2d array of the results. E.g. clothes = [[1,"name","desc"],[2,"name2","desc2"]] can you make res send a list or do you have to make a list once you have returned it?

app.get('/post', (req, res) => {
    con.connect(function(err) {
        if (err) throw err;
        var query = "SELECT * FROM products"
        con.query(query, function (err, results, fields) {
          if (err) throw err;
          var clothes = [];
          Object.keys(results).forEach(function(key) {
            let r = []
            var row = results[key];
            r.push(row.ID);
            r.push(row.name);
            r.push(row.link);
            r.push(row.imageLink);
            r.push(row.type);
            r.push(row.colour);
            r.push(row.price);
            r.push(row.brand);
            clothes.push(r);
          });  
        res.send(clothes);
  });
  });
  
});
var xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function() {
    
    clothes = this.response;
    
    document.getElementById("demo").innerHTML = clothes;
    
  };
  
  xhttp.open("GET", "http://localhost:3000/post", true);
  xhttp.send();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes of course.

Check out the official NodeJS documentation

Example:

app.get('/post', (req, res) => {
    con.connect(function(err) {
        if (err) throw err;
        var query = "SELECT * FROM products"
        con.query(query, function (err, results, fields) {
            if (err) throw err;
            var clothes = [];
            ...
            // Better set the header so the client knows what to expect; safari requires this
            res.setHeader('Content-Type', 'application/json');
            res.end(JSON.stringify(clothes));
        });
    });
});

You seem to be using the expresjs framework

Here you have a convenience method to do the above:

...
    con.query(query, function (err, results, fields) {
        if (err) throw err;
        var clothes = [];
        res.json(clothes);
    });
...

Read the response on the client side

This is a good answer on how to do this.

In short: It is recommended to use the new fetch() method on client side.

fetch("http://localhost:3000/post")
  .then(function(response) {
    return response.json();
  })
  .then(function(clothes) {
    document.getElementById("demo").innerHTML = clothes;
  });
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