✅ Create a new Vercel project: bit.ly/vercel-mdb ✅ Sign-up for a free cluster at: mdb.link/free-JIlYroSsInU ✅ Get help on our Community Forums: mdb.link/community-JIlYroSsInU
I rewrote code in js, from ts example i got, it is working in production mode on vercel, mongoDb is connecting successfully. But in Development its not, i have pulled env from vercel too, but it simply dont work
Don't you feel it's funky to have the front-end directly affect the database without some type of API? I thought a better approach to connect the React front-end to some backend (like node) and then let node deal with the HTTP requests. I don't use Next or mongo regularly, hence why I have this doubt.
What if I were to add mongo to an existing project? Would i just " npm i mongodb" and use the library as shown in the video or do i need to do more configuration?
I've tried to remove -y but ended up with TS version anyway. Is there a method to set up this template in JS? So far I've tried to add --js to the command and tsc command but didn't worked.
@@adornnet I didn't tried -n before, thanks. But in this case it didn't worked because the template doesn't have js version. I wish I would learn ts but I've got a lot in my plate at the moment.
@mphatsomtogolo6263 Unfortunately, it won't. But vercel is constantly updating their template apps on their website. You can check for other templates with js if you need a starter template to practise. Hope this helps
@@lscodeschool957 sure. here is the code. It worked for me. I tried adding console logs to test wheather it is using cached connection or it is making a new connection every time and indeed it was using cached connection. I know the code looks little weird but it works. import mongoose from 'mongoose' const MONGODB_URI = process.env.MONGO_URI // If MongoDb uri is not provided we will throw an error if (!MONGODB_URI) { throw new Error( 'Please define the MONGODB_URI environment variable inside .env.local' ) } // When we first connect to the db we will cache that connection in a variable named cached so that we don't have to connect to the database again and again on each and every request. let cached = global.mongoose // If we don't have cached connection then first we will set conn: null, promise: null if (!cached) { cached = global.mongoose = { conn: null, promise: null } } // creating an async function to connect to the db async function connectDb() { // If we have cached connection then we don't have to make connection once again. we will return the old connection. if (cached.conn) { return cached.conn } // If we don't have cached connection then we will create one and return it. if (!cached.promise) { const opts = { bufferCommands: false, } cached.promise = await mongoose.connect(MONGODB_URI, {useNewUrlParser: true}).then((mongoose) => { return mongoose }) } try { cached.conn = await cached.promise } catch (e) { cached.promise = null throw e } return cached.conn } export default connectDb
✅ Create a new Vercel project: bit.ly/vercel-mdb
✅ Sign-up for a free cluster at: mdb.link/free-JIlYroSsInU
✅ Get help on our Community Forums: mdb.link/community-JIlYroSsInU
I rewrote code in js, from ts example i got, it is working in production mode on vercel, mongoDb is connecting successfully. But in Development its not, i have pulled env from vercel too, but it simply dont work
Why it have typescript? Not in tutorial u get js files, we are now getting Ts FIles
Thank you very much!!! After spend 6 hours i finally did my server!!!
Amazing video! Thank you so much for the detailed explanation!
Thank you!
Any time!!
@MongoDB can you please update this video?
Don't you feel it's funky to have the front-end directly affect the database without some type of API?
I thought a better approach to connect the React front-end to some backend (like node) and then let node deal with the HTTP requests.
I don't use Next or mongo regularly, hence why I have this doubt.
If I want to host my project on Vercel and make it available to my users I should make my cluster whitelist ips list available to everyone?
thank you so much for clarification i will learn more about this stack
Very welcome
this is done in next framework, however I am using cra. I am not having the same options. How would I deploy my server in vercel with react?
How can I do the same without using TypeScript?
you are doing a tutorial without a link to the source code, its really stressful bruv, thanks anways
Hi Omezi, here are links to the written tutorials:
- bit.ly/3djh7fL
- bit.ly/3vNFzw4
What if I were to add mongo to an existing project? Would i just " npm i mongodb" and use the library as shown in the video or do i need to do more configuration?
my question too, i already have an existing nextjs project.
its helpfull , must integrate mongodb in vercel , not manually input it .env mong uri
Thanks!
Is this same process for django project to connect mongodb in vercel?
Dont know why but I followed your video and the sample code from Max for mongoose setup, but despite that the log prints message on each page refresh
Integrating vercel with atlas just takes too long for me, more than 10 mins +. Is this normal? It doesn't integrate properly
The command "vercel" won't be recognized. Open cmd and enter: npm install -g vercel
After that vercel command worked for me.
I've tried to remove -y but ended up with TS version anyway. Is there a method to set up this template in JS? So far I've tried to add --js to the command and tsc command but didn't worked.
have you tried replace the -y with -n ? If still not working, add additional --js after the -n
@@adornnet I didn't tried -n before, thanks. But in this case it didn't worked because the template doesn't have js version. I wish I would learn ts but I've got a lot in my plate at the moment.
@@cetinsss Got the same problem here. So this project wont work without TS?
@mphatsomtogolo6263 Unfortunately, it won't. But vercel is constantly updating their template apps on their website. You can check for other templates with js if you need a starter template to practise. Hope this helps
well this is hard to follow without nextjs, I am using vite+react
is it the same for mongoose?
may be, I am trying with mongoose
@@jewelzelal How far with this
I am having a serverless function timedout using mongoose
I try with nodejs and monggose but
404: NOT_FOUND
Code: NOT_FOUND
ID: iad1::6sgv9-1673356691534-de855ad4e4b2
It does not create for me MONGODB_URI in Vercel?!
why this cors is so difficult, I just want to continue working on something else
cors is very important for website security. Is there something specific you would like to learn about it?
your amazing👍👍
comand vercel is not recognized after installing vercel
this video should be outdated
Here is how you make a cached connection using mongoose. See replies on this comment.
I found one way to cache MongoDB connection using mongoose.... I will add that here later...
@@maxvhanamane840 Can we see what you have with mongoose
@@lscodeschool957 sure. here is the code. It worked for me. I tried adding console logs to test wheather it is using cached connection or it is making a new connection every time and indeed it was using cached connection. I know the code looks little weird but it works.
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGO_URI
// If MongoDb uri is not provided we will throw an error
if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local'
)
}
// When we first connect to the db we will cache that connection in a variable named cached so that we don't have to connect to the database again and again on each and every request.
let cached = global.mongoose
// If we don't have cached connection then first we will set conn: null, promise: null
if (!cached) {
cached = global.mongoose = { conn: null, promise: null }
}
// creating an async function to connect to the db
async function connectDb() {
// If we have cached connection then we don't have to make connection once again. we will return the old connection.
if (cached.conn) {
return cached.conn
}
// If we don't have cached connection then we will create one and return it.
if (!cached.promise) {
const opts = {
bufferCommands: false,
}
cached.promise = await mongoose.connect(MONGODB_URI, {useNewUrlParser: true}).then((mongoose) => {
return mongoose
})
}
try {
cached.conn = await cached.promise
} catch (e) {
cached.promise = null
throw e
}
return cached.conn
}
export default connectDb
@@maxvhanamane840 Ok I will try it in my code and get back with some findings if it has worked
@@maxvhanamane840 Cool it has worked .... Thanks
this one still uses pages router
🇽🇰 👋
Many thanks. I try with nodejs but 404: NOT_FOUND
Code: NOT_FOUND
ID: iad1::6sgv9-1673356691534-de855ad4e4b2