wbrandon25 / Online-Crash-Gambling-Simulator

Online Crash Gambling Game Web App Built with the MERN Stack
https://effulgent-travesseiro-c05997.netlify.app
109 stars 81 forks source link

FIXED - Cannot read properties of null (reading 'previous_crashes') #6

Open BustAces opened 1 year ago

BustAces commented 1 year ago

FIXED I got the game running. I solved the issue this way.

1. At server.js add the following code after the MongoDB connection line:

const crashSchema = new mongoose.Schema( { round_number: Number, active_player_id_list: [String], multiplier_crash: Number, b_betting_phase: Boolean, b_game_phase: Boolean, b_cashout_phase: Boolean, time_now: Number, previous_crashes: [Number], round_id_list: [Number], chat_messages_list: [] } );

const Crash = mongoose.model( 'Crash', crashSchema );

Crash.create({ round_number: 1, active_player_id_list: [], multiplier_crash: 0, b_betting_phase: false, b_game_phase: false, b_cashout_phase: false, time_now: -1, previous_crashes: [], round_id_list: 0, chat_messages_list: [] }) .then(result => { console.log(result) });

Save it and run the server. It will now automatically generate the documents. NOTE: The server will crash thats OK!

2. Now, copy the ID of the document and paste it in server.js at GAME_LOOP_ID. 3. Go to /models/game_loop.js and change: _const gameloop = new mongoose.Schema => const crashSchema = new mongoose.Schema _mongoose.model("game_loop", gameloop); => mongoose.model("Crash", crashSchema);

4. In the root folder, create an .evn file with the following line: PASSPORT_SECRET = "YOURPASSWORDHERE"

5. Go back to server.js and delete or comment out the code you added in step 1. 6. Restart the server and enjoy. This is how it worked for me.

Original message: Noticed the other issues claiming the same but without a clear answer still.. App does connect to my mongodb, i can see it in the connections list, however.. schema's arent generated even though the app has permission. Adding the game_loop schema manually also not solves the problem

BustAces commented 1 year ago

To double check that the connection to mongodb is successfully, i added a console log. No errors and connection is successfull, yet.. still same error.

mbebs commented 1 year ago

I have same issue. Anyone knows how to create schemas on the MongoDB?

SWS-5007 commented 1 year ago

I am getting the same error. How to fix this?

backend@1.0.0 start nodemon server.js

[nodemon] 2.0.18 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching extensions: js,mjs,json [nodemon] starting node server.js E:\Online-Crash-Gambling-Simulator\backend\server.js:412 io.emit('crash_history', theLoop.previous_crashes) ^

TypeError: Cannot read properties of null (reading 'previous_crashes') at loopUpdate (E:\Online-Crash-Gambling-Simulator\backend\server.js:412:40) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Timeout._onTimeout (E:\Online-Crash-Gambling-Simulator\backend\server.js:351:3) [nodemon] app crashed - waiting for file changes before starting...