Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read mongo database after deployment on heroku, returns 503 error, but it works locally

After executing npm run build and having fixed path bugs on my server.js it works nice in localhost. But once I deploy it to Heroku the data never charges and ends up with these messages in chrome console:

Failed to load resource: the server responded with a status of 503 (Service Unavailable)

This is followed by another error:

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0.

I guess the first error is related with the routes as it is trying to fetch data and the server response is a 503, and I guess the second one is still related with the used path for static files on my server.js file.

The code is available here: https://github.com/pepdbm7/Meetup-with-Vue

What I already tried for the first error is: change the routes in the client side (adding the herokuapp URL as the root URL for every fetch()), and also the server side routes, but didn't help.

It works perfectly locally like it is now, that's what I don't understand.

...and for the second error I changed the paths to: res.sendFile(path.join(__dirname, "client/dist")) and to: app.use(express.static("client/dist"));, and I have already tried in other ways but then it didn't work at all as it couldn't access to the minified js and CSS files.

As additional information, I did control the heroku logs when deploying and this is the result logs, I don't see major errors but just warnings, maybe I am missing something here:

2019-06-28T15:32:25.610341+00:00 app[api]: Deploy d1938a6e by user [email protected]
2019-06-28T15:32:25.610341+00:00 app[api]: Release v17 created by user [email protected]
2019-06-28T15:32:26.000000+00:00 app[api]: Build succeeded
2019-06-28T15:32:27.448688+00:00 heroku[web.1]: Restarting
2019-06-28T15:32:27.765993+00:00 heroku[web.1]: State changed from up to starting
2019-06-28T15:32:28.597600+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-06-28T15:32:28.706141+00:00 heroku[web.1]: Process exited with status 143
2019-06-28T15:32:32.750630+00:00 heroku[web.1]: Starting process with command `npm start`
2019-06-28T15:32:34.977483+00:00 app[web.1]: 
2019-06-28T15:32:34.977500+00:00 app[web.1]: > [email protected] start /app
2019-06-28T15:32:34.977501+00:00 app[web.1]: > nodemon server.js
2019-06-28T15:32:34.977502+00:00 app[web.1]: 
2019-06-28T15:32:35.398126+00:00 app[web.1]: [33m[nodemon] 1.19.1[39m
2019-06-28T15:32:35.399744+00:00 app[web.1]: [33m[nodemon] to restart at any time, enter `rs`[39m
2019-06-28T15:32:35.400250+00:00 app[web.1]: [33m[nodemon] watching: *.*[39m
2019-06-28T15:32:35.401055+00:00 app[web.1]: [32m[nodemon] starting `node server.js`[39m
2019-06-28T15:32:36.388146+00:00 heroku[web.1]: State changed from starting to up
2019-06-28T15:32:35.987818+00:00 app[web.1]: node environment is production
2019-06-28T15:32:35.993171+00:00 app[web.1]: meetup_app_vue 1.0.0 up and running on port 40353
2019-06-28T15:32:35.994993+00:00 app[web.1]: (node:36) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
2019-06-28T15:32:35.994996+00:00 app[web.1]:     at parseConnectionString (/app/node_modules/mongodb-core/lib/uri_parser.js:505:21)
2019-06-28T15:32:35.994998+00:00 app[web.1]:     at connect (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:195:3)
2019-06-28T15:32:35.994999+00:00 app[web.1]:     at connectOp (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
2019-06-28T15:32:35.995001+00:00 app[web.1]:     at executeOperation (/app/node_modules/mongodb/lib/utils.js:416:24)
2019-06-28T15:32:35.995002+00:00 app[web.1]:     at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:175:10)
2019-06-28T15:32:35.995004+00:00 app[web.1]:     at Promise (/app/node_modules/mongoose/lib/connection.js:554:12)
2019-06-28T15:32:35.995006+00:00 app[web.1]:     at new Promise (<anonymous>)
2019-06-28T15:32:35.995007+00:00 app[web.1]:     at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:551:19)
2019-06-28T15:32:35.995009+00:00 app[web.1]:     at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:289:15)
2019-06-28T15:32:35.995010+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:23:4)
2019-06-28T15:32:35.995012+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:776:30)
2019-06-28T15:32:35.995013+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
2019-06-28T15:32:35.995015+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:653:32)
2019-06-28T15:32:35.995016+00:00 app[web.1]:     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
2019-06-28T15:32:35.995017+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
2019-06-28T15:32:35.995019+00:00 app[web.1]:     at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
2019-06-28T15:32:35.995111+00:00 app[web.1]: (node:36) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
2019-06-28T15:32:35.995194+00:00 app[web.1]: (node:36) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Whatever it is it has to be a very simple thing, but I already spent some days to find it so any clue will be very welcome!

Edit: as some comments say, I had to put the environment variables on heroku, which I just missed or I didn't name properly. Thanks!

like image 313
Pepdbm 7 Avatar asked Jan 24 '26 22:01

Pepdbm 7


1 Answers

As I expected it had to be a simple-solution problem and it was so. I just had to update the environment variables in Config Vars in Heroku Settings, as some of the comments say! Now is working fine!!

Thanks a lot!

like image 165
Pepdbm 7 Avatar answered Jan 27 '26 16:01

Pepdbm 7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!