hoangvvo / nextjs-mongodb-app

A Next.js and MongoDB web application, designed with simplicity for learning and real-world applicability in mind.
https://nextjs-mongodb.now.sh/
MIT License
1.53k stars 286 forks source link

error 500 & 502 on SignUp #5

Closed ivanjeremic closed 4 years ago

ivanjeremic commented 5 years ago

I experiment currently with Nextjs and mongodb and then saw your tutorial, my App is now up and running but nobody can SignUp i get the error POST https://mausys-web.ivanjeremic.now.sh/api/users 500 mongoURI .env and cloudinary URI are all set and ready to go. any idea whats the problem here? Thanks

Here the link to my Site https://mausys-web.ivanjeremic.now.sh/

sometime I get this error also commons.0be659ca3dbe117d9b15.js:1 POST https://mausys-web.ivanjeremic.now.sh/api/users 502

Error: Connection strategy not found at new MongoStore (D:\Development\Apps\mausys-web\node_modules\connect-mongo\src\index.js:129:15) at useSession (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\session.js:159:10) at middleware (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\session.js:185:55) at Module.V034 (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\users.js:257:146) at __webpack_require__ (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\users.js:23:31) at Object.5 (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\users.js:99:18) at __webpack_require__ (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\users.js:23:31) at D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\users.js:91:18 at Object.<anonymous> (D:\Development\Apps\mausys-web\.next\server\static\QdBxRGfyRVGSOG2ufP6LY\pages\api\users.js:94:10) at Module._compile (internal/modules/cjs/loader.js:778:30)

Still can not fix it.

hipdev commented 5 years ago

Hi @ivanjeremic , is because in the repo there is no way to get the env vars right now, please do this:

npm install dotenv

Then in the file useMiddleware.js put:

import 'dotenv/config';   // This new line
import useDatabase from './useDatabase';
import useSession from './useSession';
import useAuthentication from './useAuthentication';

const middleware = handler =>
  useDatabase(useSession(useAuthentication(handler)));

export default middleware;

and create a .env file in the root like: (Check the file starts with a dot --> .env)

MONGODB_URI="mongodb+srv://user:pass@testing-j4duk.mongodb.net/dbname?retryWrites=true&w=majority"

Also in the .env file you can put the Cloudinary path if you want to try that part too.

I hope it helps you

ivanjeremic commented 4 years ago

@julianux In the GitHub repo there was no dotenv thats why I thought it is not needed. I have now followed the steps you told me and now the console trows this error.

(node:30196) UnhandledPromiseRejectionWarning: MongoError: not authorized on admin to execute command { createIndexes: "sessions", indexes: [[{name expires_1} {key [{expires 1}]} {expireAfterSeconds 0}]], writeConcern: { w: "majority" }, lsid: { id: {4 [156 192 7 101 240 33 65 202 157 135 193 15 223 161 150 61]} }, $clusterTime: { clusterTime: 6745668904883847169, signature: { hash: [171 121 241 5 43 171 222 78 87 184 251 130 146 173 164 149 95 161 175 75], keyId: 6742367827379879936.000000 } }, $db: "admin" }

EDIT The console error above I fixed by following this discussion https://stackoverflow.com/questions/54986716/node-js-mongoerror-not-authorized-on-admin-to-execute-command

Now the console error is fixed but same MongoStore error comming up like from the beginning.

Error: Connection strategy not found at new MongoStore (D:\Development\Apps\mausys-web\node_modules\connect-mongo\src\index.js:129:15) at useSession (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:201:10) at middleware (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:177:151) at Module../pages/api/users.js (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:284:138) at webpack_require (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:23:31) at Object.7 (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:295:18) at webpack_require (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:23:31) at D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:91:18 at Object. (D:\Development\Apps\mausys-web.next\server\static\development\pages\api\users.js:94:10) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18)

hoangvvo commented 4 years ago

@ivanjeremic Sorry for the lack of information, I just update README with the link to my article on the issue.

Where, which platform do you deploy your code? There might be an option to config environmental variable there. (After all, .env is only used for development)

hoangvvo commented 4 years ago

@ivanjeremic I also read the SO solution. It does not seem to be the case, I use mongodb+srv:// variation in connect-mongo, and it works just fine. Perhaps, check your Atlas user setting? (Did you allow the user to READ+WRITE?)

ivanjeremic commented 4 years ago

Yes checked that as well everything fine, but still not working. I deploy on ZEIT maybe I need to set the secrets on the now platform because I use the newest version of Nextjs. https://zeit.co/blog/environment-variables-secrets

By the way any reason you go with session and not jsonwebtoken?

hoangvvo commented 4 years ago

@ivanjeremic http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/ This article does a good job explaining the reasons. JWT is not bad. The way people misunderstanding and misusing it is bad. I saw a lot of JWT articles and most of them incorrectly use JWT.

hoangvvo commented 4 years ago

@ivanjeremic please let me know if the problem is not resolved.

hoangvvo commented 4 years ago

Hey @ivanjeremic, would love to have an update. Tell me if I can help in any way!

ivanjeremic commented 4 years ago

Hey @ivanjeremic, would love to have an update. Tell me if I can help in any way!

I will upload later today my repo that shows how I habe it and reproduces the error.

hoangvvo commented 4 years ago

Closing: Already resolved a long time ago with doc update.