can someone tell me what this error mean?

Issue

MongoError: cannot do raw queries on admin in atlas
i tried to run :

 mongoose
  .connect(
    'mongodb+srv://yonco:mypassword@shop.iujhp.mongodb.net/?retryWrites=true&w=majority'
  )
  .then(result => {
    User.findOne().then(user => {
      if (!user) {
        const user = new User({
          name: 'yonc',
          email: 'yonc@rest.com',
          cart: {
            items: []
          }
        });
        user.save();
      }
    });
    app.listen(3000);
  })
  .catch(err => {
    console.log(err);
  });

i actully copied the source code from the course im doing but the error remain

Solution

Your string should look like this:

mongodb://<user>:<password>@cluster0-shard-00-00-rnmsm.mongodb.net:27017,cluster0-shard-00-01-rnmsm.mongodb.net:27017,cluster0-shard-00-02-rnmsm.mongodb.net:27017/test?ssl=true&replicaSet=cluster0-shard-0&authSource=admin&retryWrites=true&w=majority

Use your username and password correctly.

I would suggest make env for connection to MongoDB url as it contains user details.
env docs : https://www.twilio.com/blog/working-with-environment-variables-in-node-js-html

Hope this helps!
Please upvote if you like.

Answered By – Ausaf Ahmad

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.