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

Cannot connect to MongoDB #124

Closed danielmeeusen closed 3 years ago

danielmeeusen commented 3 years ago

This might just be a personal issue, but recently any app that I've created based off this template or even if I just download the template bare and try and run it I get this error whenever I try to make any kind of request to MongoDB:

TypeError [ERR_INVALID_URL]: Invalid URL: undefined at onParseError (internal/url.js:279:9) at new URL (internal/url.js:355:5) at Object../pages/api/user/index.js (C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\pages\api\user.js:77:5) at webpack_require (C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\webpack-runtime.js:25:42) at webpack_exec (C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\pages\api\user.js:268:52) at C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\pages\api\user.js:269:98 at Function.__webpack_require__.X (C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\webpack-runtime.js:108:21) at C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\pages\api\user.js:269:47 at Object. (C:\Users\danme\Desktop\nextjs-mongodb-app-master.next\server\pages\api\user.js:272:3) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) at Function.Module._load (internal/modules/cjs/loader.js:778:12) at Module.require (internal/modules/cjs/loader.js:961:19) at require (internal/modules/cjs/helpers.js:92:18) at DevServer.handleApiRequest (C:\Users\danme\Desktop\nextjs-mongodb-app-master\node_modules\next\dist\next-server\server\next-server.js:66:181) { input: 'undefined', code: 'ERR_INVALID_URL' }

I have been able to connect basic next.js apps to MongoDB just fine but for somereason whenever I use swr or next-connect I get the same exact error.

Any help would be appreicated. Thanks.

hoangvvo commented 3 years ago

Did you add the environment variable for MongoDB? https://github.com/hoangvvo/nextjs-mongodb-app#environmental-variables

itsOllez commented 3 years ago

Had the same error. As the error is triggered within user.js, adding your own CLOUDINARY_URL within the environment variables solved this issue for me.

hoangvvo commented 3 years ago

Just a note in the README that I include my own .env to get started quickly:

I include my own environment variables in .env.example for experimentation purposes. Please replace them with your owns and refrain from sabotaging them. You can try them in development by renaming it into .env.

danielmeeusen commented 3 years ago

Sorry I should have been more specific. All attempts were being made with all of my own enviroment variables.

I found a forum where someone said they fixed a similar problem by changing MongoDB connection string to node version 2.2.12 or later and it worked.

The post did not tell me why this would work so I would still be very interested if anyone has any idea what has changed or is going wrong in my dev enviroment that would require this. I have node 14.17.5 installed along with both 2017 and 2019 Visual Studio Build Tools.

hoangvvo commented 3 years ago

I'm not sure what you meant by "changing MongoDB connection string to node version 2.2.12 or later". Could you clarify?

danielmeeusen commented 3 years ago

When you are generating your database connection string on MongoDB.com you can choose what version of Node.js to use and I downgraded mine from Node version 4.0 to 2.2.12 or later.

hoangvvo commented 3 years ago

I wonder if you are installing an older version of mongodb. Try checking package.json. Also try delete the lock file, node_modules and try again.

danielmeeusen commented 3 years ago

"mongodb": "^3.6.11"

I'm pretty sure this has to do with the connectivity of my personal development enviroment as no one else has experienced this problem. I found a work around for now and will close this issue if it's okay with you.

ktisakib commented 3 years ago

I am having the same problem here

error - TypeError [ERR_INVALID_URL]: Invalid URL: undefined at onParseError (internal/url.js:279:9) at new URL (internal/url.js:355:5) at Object../pages/api/user/index.js (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:434:5) at webpack_require (D:\Web-design\nextjs-mongodb-app.next\server\webpack-runtime.js:25:42) at webpack_exec (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:614:39) at D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:615:28 at Object. (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:618:3) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) { input: 'undefined', code: 'ERR_INVALID_URL' }

hoangvvo commented 3 years ago

I am having the same problem here

error - TypeError [ERR_INVALID_URL]: Invalid URL: undefined at onParseError (internal/url.js:279:9) at new URL (internal/url.js:355:5) at Object../pages/api/user/index.js (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:434:5) at webpack_require (D:\Web-design\nextjs-mongodb-app.next\server\webpack-runtime.js:25:42) at webpack_exec (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:614:39) at D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:615:28 at Object. (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:618:3) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) { input: 'undefined', code: 'ERR_INVALID_URL' }

Did you set your environment variable? It seems to say the input is undefined.

danielmeeusen commented 3 years ago

When you are generating your database connection string on MongoDB.com you can choose what version of Node.js to use, try downgrading from Node version 4.0 to 2.2.12 or later. Still not sure why, but this solved the issue for me.

hoangvvo commented 3 years ago

When you are generating your database connection string on MongoDB.com you can choose what version of Node.js to use, try downgrading from Node version 4.0 to 2.2.12 or later. Still not sure why, but this solved the issue for me.

Could you send me your package.json? The newest breaking version of NodeJS mongodb dep (v4) does not work. I'm planning to fix soon.

danielmeeusen commented 3 years ago

{ "name": "private-for-business-reasons", "version": "0.0.1", "private": true, "engines": { "node": "14.16.0" }, "local_scripts": { "dev": "next dev", "build": "next build", "start": "next start" }, "scripts": { "dev": "next", "build": "next build", "start": "next start -p $PORT" }, "dependencies": { "@material-ui/core": "^4.12.3", "@material-ui/icons": "^4.11.2", "@material-ui/lab": "^4.0.0-alpha.60", "@sendgrid/mail": "^7.4.6", "bcryptjs": "^2.4.3", "cloudinary": "^1.27.0", "clsx": "^1.1.1", "connect-mongo": "^3.2.0", "express-session": "^1.17.2", "mongodb": "^3.7.1", "multer": "^1.4.3", "nanoid": "^3.1.25", "next": "^10.2.3", "next-connect": "^0.9.1", "passport": "^0.4.1", "passport-local": "^1.0.0", "prop-types": "^15.7.2", "react": "17.0.1", "react-dom": "17.0.1", "serverless-mysql": "^1.5.4", "swr": "^0.5.7", "validator": "^13.6.0" }, "devDependencies": { "dotenv": "^8.6.0" } }

hoangvvo commented 3 years ago

nothing seems off tbh, what is the version of your mongodb server?

danielmeeusen commented 3 years ago

I'm using mongodb atlas.

danielmeeusen commented 3 years ago

I'm fairly certain this is a windows development enviroment problem as all of my deployed apps work just fine they just have this problem when running locally.

hoangvvo commented 3 years ago

I am having the same problem here

error - TypeError [ERR_INVALID_URL]: Invalid URL: undefined at onParseError (internal/url.js:279:9) at new URL (internal/url.js:355:5) at Object../pages/api/user/index.js (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:434:5) at webpack_require (D:\Web-design\nextjs-mongodb-app.next\server\webpack-runtime.js:25:42) at webpack_exec (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:614:39) at D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:615:28 at Object. (D:\Web-design\nextjs-mongodb-app.next\server\pages\api\user.js:618:3) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) { input: 'undefined', code: 'ERR_INVALID_URL' }

@ktisakib Sorry I missed your messsage. Also please don't post your environment variables ( I deleted the message ). Could you try again with the latest commit to this template? If you cannot afford recloning/recreate using the template, please see the changes in https://github.com/hoangvvo/nextjs-mongodb-app/pull/125. Let me know if I can help anyhow.

hoangvvo commented 3 years ago

I'm fairly certain this is a windows development enviroment problem as all of my deployed apps work just fine they just have this problem when running locally.

I will try to boot into a window machine and try it out if I have the time! Also I upgrade the version of mongodb in this PR https://github.com/hoangvvo/nextjs-mongodb-app/pull/125 if you want to check it out. It might solve the issue.