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

178
Visualizações
How do I parse one part of a nested query result? (Javascript and MySQL)

I am trying to get a nested structure from a MySQL query, and as a result of using JSON_OBJECT, one section of my return data is stringified. The data I am currently getting looks as follows:

[
    {
        "id": 3,
        "body": "Does this product run big or small?",
        "date_written": 1608535907,
        "asker_name": "jbilas",
        "helpful": 8,
        "reported": 0,
        "answers": null
    },
    {
        "id": 4,
        "body": "How long does it last?",
        "date_written": 1594341317,
        "asker_name": "funnygirl",
        "helpful": 6,
        "reported": 0,
        "answers": "{\"65\": {\"id\": 65, \"body\": \"It runs small\", \"date\": 1605784307, \"helpful\": 1, \"reported\": 0, \"answerer_name\": \"dschulman\"}, \"89\": {\"id\": 89, \"body\": \"Showing no wear after a few months!\", \"date\": 1599089610, \"helpful\": 8, \"reported\": 0, \"answerer_name\": \"sillyguy\"}}"
    },
    {etc},
    {etc}]

Before sending the result of my database query back to the client, I need to parse the answers value. My initial thought was to make the result a promise, then use a forEach loop to run through each object and parse 'answers' if it is not undefined. My code to do that is organized as follows:

db.query(query, [req.query['product_id'], req.query['count']], (error, results) => {
    if (error) {
      console.log('err err ', error);
    }
    if (!results[0]) {
      res.json('No questions found')
    } else {
      console.log(results)
      Promise.resolve(results.forEach(result => {
        if (result['answers'] !== undefined) {
          JSON.parse(result['answers']);
        }
        return results;
      }))
      .then(results => {
        console.log(results)
      })

      res.json(results.splice(0, req.query['count']));
    }
  })

The console log in my 'then' block returns undefined. I also tried using results.map(), but this logged a bunch of empty arrays in the then block. Any advice on how to parse the 'answers' values when they are not null for an array of objects much appreciated!

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

0

Just a thought, have you tried just iterating results and updating the answers property without a Promise?:

db.query(query, [req.query['product_id'], req.query['count']], (error, results) => {
  if (error) {
    console.log('err err ', error);
  }
  if (!results[0]) {
    res.json('No questions found')
  } else {
    console.log(results)

    results.forEach(result => {
      result.forEach((obj) => {
        obj.answers = JSON.parse(obj.answers);
      });
    });

    res.json(results.splice(0, req.query['count']));
  }
});
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