timotheeg / nestrischamps

A web-based OCR and restreamer system for NES Classic Tetris players
MIT License
45 stars 11 forks source link

"My scores and replays" TypeError. das_avg provided as string without toFixed method. #129

Closed zohassadar closed 1 year ago

zohassadar commented 1 year ago

I'm running this locally in a docker container on a Windows 10 machine with WSL2 and using the latest branch of main. I tried the docker images node:latest (currently 18.7.0) and node:14.20.0 with the same results.

http://127.0.0.1:5000/stats/scores gives the following error:

TypeError: /code/views/scores.ejs:66
    64|                 <td class="data-number" align="right"><%= score.num_droughts %></td>
    65|                 <td class="data-number" align="right"><%= score.max_drought %></td>
 >> 66|                 <td class="data-number" align="right"><%= (score.das_avg || 0).toFixed(1) %></td>
    67|                 <td class="data-number" align="right">
    68|                     <%=
    69|                         Math.floor(score.duration / 1000 / 60)

(score.das_avg || 0).toFixed is not a function
    at eval (eval at compile (/code/node_modules/ejs/lib/ejs.js:662:12), <anonymous>:53:47)
    at Array.forEach (<anonymous>)
    at eval (eval at compile (/code/node_modules/ejs/lib/ejs.js:662:12), <anonymous>:18:15)
    at scores (/code/node_modules/ejs/lib/ejs.js:692:17)
    at tryHandleCache (/code/node_modules/ejs/lib/ejs.js:272:36)
    at View.exports.renderFile [as engine] (/code/node_modules/ejs/lib/ejs.js:489:10)
    at View.render (/code/node_modules/express/lib/view.js:135:8)
    at tryRender (/code/node_modules/express/lib/application.js:640:10)
    at Function.render (/code/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/code/node_modules/express/lib/response.js:1017:7)

After poking at it, the value of score.das_avg looks like it's being provided as a string instead of a float.

I was able to work around by wrapping score.das_avg in parseFloat() in the line referenced by the error. The page renders correctly with this edit.

Please let me know if there's any other information I can provide.

timotheeg commented 1 year ago

Hey, thanks for opening an issue. This is a bit weird considering things work in prod and in my local setup 🤔. I'll take a look when I get home.

What version of postgres are you using?

zohassadar commented 1 year ago

PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

timotheeg commented 1 year ago

Riiiight. I just checked the prod DB, looks like I updated the datatype at some point but forgot to update setup/db.sql 🤦

Both fields das_avg's and tetris_rate are of type double precision in prod (NOT numeric), which is why they are converted to JS floats by the postgres driver. It made sense that the numeric format would not be converted, and return a string, to ensure there's no loss in precision. That's probably why I dropped it.

I just pushed a commit to correct the DB setup file.

Sorry about that. Goes to show pretty much nobody has a local setup of NTC 😢 .

Thanks again for the report!

zohassadar commented 1 year ago

It was an effective way to learn about the peculiarities of different kinds of numbers in postgres.

Using your explanation combined with google, I was able to bring my existing DB up to speed with the following:

ALTER TABLE scores
ALTER COLUMN tetris_rate TYPE DOUBLE PRECISION,
ALTER COLUMN das_avg TYPE DOUBLE PRECISION;

Using an unmodified ./views/scores.ejs afterwards I was able to load the scores page with no issue.

Thanks!!