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

293
Visualizações
This function becomes undefined and I don't know why

I am trying to make a program using javascript, EJS, and SQL(PostgreSQL) but I am having an issue that crashes my server making me unable to go further.

.query(SELECT * FROM public.games WHERE id= ${highscore.gameid}) ^ TypeError: Cannot read properties of undefined (reading 'gameid')

Here is my error

I have tried to delete data from my database and tried to change this function I also tried to remove it but it just messed up my code even more. Also weirdly enough, my code worked, but the next day I got this error.

const getHighscore = async (id) => {
  let highscore;
  const pool = new Pool(credentials);

  await pool
    .query(`SELECT * FROM public.highscores WHERE id= ${id}`)
    .then((res) => {
      console.log(res.rows[0]);
      highscore = res.rows[0];
    });
  await pool
    .query(`SELECT * FROM public.games WHERE id= ${highscore.gameid}`)
    .then((res) => (highscore.game = res.rows[0]));

  return highscore;
};

Console.log(res.row[0]) = undefined

Console.log(res) = Result {
  command: 'SELECT',
  rowCount: 0,
  oid: null,
  rows: [],
  fields: [
    Field {
      name: 'id',
      tableID: 24601,
      columnID: 1,
      dataTypeID: 23,
      dataTypeSize: 4,
      dataTypeModifier: -1,
      format: 'text'
    },
    Field {
      name: 'username',
      tableID: 24601,
      columnID: 2,
      dataTypeID: 1043,
      dataTypeSize: -1,
      dataTypeModifier: 34,
      format: 'text'
    },
    Field {
      name: 'gameid',
      tableID: 24601,
      columnID: 3,
      dataTypeID: 23,
      dataTypeSize: 4,
      dataTypeModifier: -1,
      format: 'text'
    },
    Field {
      name: 'score',
      tableID: 24601,
      columnID: 4,
      dataTypeID: 23,
      dataTypeSize: 4,
      dataTypeModifier: -1,
      format: 'text'
    },
    Field {
      name: 'active',
      tableID: 24601,
      columnID: 5,
      dataTypeID: 16,
      dataTypeSize: 1,
      dataTypeModifier: -1,
      format: 'text'
    },
    Field {
      name: 'date',
      tableID: 24601,
      columnID: 6,
      dataTypeID: 1082,
      dataTypeSize: 4,
      dataTypeModifier: -1,
      format: 'text'
    }
  ],
  _parsers: [
    [Function: parseInteger],
    [Function: noParse],
    [Function: parseInteger],
    [Function: parseInteger],
    [Function: parseBool],
    [Function: parseDate]
  ],
  _types: TypeOverrides {
    _types: {
      getTypeParser: [Function: getTypeParser],
      setTypeParser: [Function: setTypeParser],
      arrayParser: [Object],
      builtins: [Object]
    },
    text: {},
    binary: {}
  },
  RowCtor: null,
  rowAsArray: false
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on your logs the error seems to be as expected. You're setting highscore to res.rows[0], which is undefined. Therefore, no gameid property exists on highscore, hence the error.

If res.rows is what's supposed to contain your data, then basically right now you're not getting back the needed data or it doesn't exist.

A better approach might be to make this query ONLY if highscore is NOT undefined or null, and the gameid property exists:

if (highscore && highscore.hasOwnProperty('gameid'))
 await pool
    .query(`SELECT * FROM public.games WHERE id= ${highscore.gameid}`)
    .then((res) => (highscore.game = res.rows[0]));

Remember though, highscore will still get returned even if it is undefined, so your function might return undefined

You could also use optional chaining instead

`SELECT * FROM public.games WHERE id= ${highscore?.gameid}`

Doing this will avoid error because of highscore being undefined, but it will be an unnecessary call to query

Hope it helps.

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