Skip to content

[Fixed] Access Denied Issue #109

@sergomen

Description

@sergomen

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions