-
Notifications
You must be signed in to change notification settings - Fork 441
Open
Description
If you are still struggling with this error, you can do as I did.
models/user.js:
username: {
type: String,
required: [true, 'Username is required!'],
// changed it from 8,20 to 3,20
match: [/^(?=.{3,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._\u0400-\u04FF]+(?<![_.])$/, "Username invalid, it should contain 3-20 alphanumeric letters and be unique!"]
// [/^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/, "Username invalid, it should contain 8-20 alphanumeric letters and be unique!"]
},utils/database.js:
import mongoose from 'mongoose';
let isConnected = false;
export const connectToDB = async () => {
// mongoose.SchemaTypes('strictQuery', true); <-------------- mongoose.SchemaTypes is not a function in Mongoose
if (isConnected) {
console.log('MongoDB is already connected');
return; // to stop if it's running
}
try {
await mongoose.connect(process.env.MONGODB_URI, {
dbName: "share-prompt",
// useNewUrlParser: true, <-------------- deprecated
// useUnifiedTopology: true, <-------------- deprecated
});
isConnected = true;
console.log('MongoDB connected');
} catch (error) {
console.log(error);
}
}Also, other people make a mistake in .env file. You have to write username and password for you mongodb string without <>:
MONGODB_URI=mongodb+srv://username:[email protected]/?retryWrites=true&w=majority&appName=Cluster0
Hope it will help. Happy coding.
Metadata
Metadata
Assignees
Labels
No labels