skip to Main Content

I am unable to access MongoDB database for some reason. I have run through the usual suspects such as removing <> brackets around the password and also making my cluster accessible from any IP address.
I am trying to connect to one of the sample databases provided by mongoDB ‘sample_mflix’, I am not sure if this is an issue. In the tutorial I was referring to https://www.youtube.com/watch?v=n1mdAPFq2Os&t=887s the creator leaves the database as ‘test’ and it works, but i tried this and failed also.
Any help would be much appreciated.

const { ApolloServer } = require('apollo-server');
const gql = require('graphql-tag');
const mongoose = require ('mongoose');

const { MONGODB } = require('./config.js');

const typeDefs = gql`
    type Query{
        sayHi: String!
    }
`;
//each query has resolver
const resolvers = {
    Query: {
        sayHi: () => 'Hello World!!!'
    }   
};

const server = new ApolloServer({
    typeDefs,
    resolvers
});

mongoose.connect('mongodb+srv://MYUSERNAME:[email protected]/**sSAMPLEDATABASEgivenbyMongoDB**?retryWrites=true&w=majority', {useNewUrlParser: true})
    .then(() => {
        console.log('MongoDB connected');
        return server.listen({ port: 5000 });
    })
    .then(res => {
        console.log(`Server running at ${res.url}`);
    });

2

Answers


  1. Chosen as BEST ANSWER

    I am not sure what happened but I did nothing and suddenly it works


  2. If your password contains non-alphanumeric character such as @, #, & you need to make sure you pass the password through encodeURIComponent otherwise mongodb could misinterpret the password the password and therefore reject it.

    MYUSERNAME:encodeURIComponent(MYPASSWORD)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search