Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
Esta función se vuelve indefinida y no sé por qué.

Estoy tratando de hacer un programa usando javascript, EJS y SQL (PostgreSQL), pero tengo un problema que bloquea mi servidor y no puedo continuar.

.query( SELECT * FROM public.games WHERE id= ${highscore.gameid} ) ^ TypeError: No se pueden leer las propiedades de undefined (leyendo 'gameid')

Aquí está mi error

Intenté eliminar datos de mi base de datos e intenté cambiar esta función. También intenté eliminarla, pero mi código se arruinó aún más. También, curiosamente, mi código funcionó, pero al día siguiente recibí este 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 answers
Answer question

0

Según sus registros, el error parece ser el esperado. Está configurando la highscore más alta en res.rows[0] , que no está definido . Por lo tanto, no existe ninguna propiedad gameid en highscore , de ahí el error.

Si res.rows es lo que se supone que contiene sus datos, entonces, básicamente, en este momento no está recuperando los datos necesarios o no existe.

Un mejor enfoque podría ser hacer esta consulta SOLO si la highscore más alta NO es indefinida o nula, y existe la propiedad gameid :

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

Sin embargo, recuerde que la highscore más alta aún se devolverá incluso si no está definida , por lo que su función podría devolverla sin definir

También podría usar el encadenamiento opcional en su lugar

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

Hacer esto evitará errores debido a que la highscore más alta no está definida, pero será una llamada innecesaria para consultar

Espero eso ayude.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!