1

I am getting this error when i am trying to insert anything into Mongo db. Any help would be appreciated.

const mongoose = require('mongoose');
const dbpath = "mongodb+srv://cluster0-bm7js.mongodb.net/classic";

mongoose.connect(dbpath, {useUnifiedTopology: true , useNewUrlParser: true })
  .then(()=> console.log("Now connected to MongoDB!"))
  .catch(err=> console.error("Something went wrong", err));

const gameSchema = new mongoose.Schema( {
  title: String, 
  publisher: String,
  tags: [String],
  date: { 
    type: Date,
    default: Date.now
  },
  onSale: Boolean,
  price: Number
});

const Game = mongoose.model('Game', gameSchema);

async function saveGame() {
  const game = new Game( { 
    title: "Tekken 3",
    publisher: "Neogeo",
    tags: ["adventure", "action"],
    onSale: false,
    price: 69.99,
  });

  const result = await game.save();
  console.log(result);

}

saveGame();

This is my code and the error i am getting after running the above code is as,

(node:94819) UnhandledPromiseRejectionWarning: MongoError: user is not allowed to do action [insert] on [classic.games]
    at Connection.<anonymous> (/Users/thinkun/Desktop/mongo/node_modules/mongodb/lib/core/connection/pool.js:466:61)
    at Connection.emit (events.js:198:13)
    at processMessage (/Users/thinkun/Desktop/mongo/node_modules/mongodb/lib/core/connection/connection.js:364:10)
    at TLSSocket.<anonymous> (/Users/thinkun/Desktop/mongo/node_modules/mongodb/lib/core/connection/connection.js:533:15)
    at TLSSocket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:94819) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:94819) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Not sure why Mongo db is unable to help me working inside.

Thanks in advance.

4
  • 1
    The error is telling you your user is not authorized. How is your user configured? Commented Oct 1, 2019 at 23:37
  • @Jacob The user is configured as Atlas admin. I am actually using this connection with Atlas. Commented Oct 1, 2019 at 23:42
  • Possible duplicate stackoverflow.com/a/47774997/3667121 Commented Oct 1, 2019 at 23:48
  • @Deda doesn't works for me. I've tried out the driver but it's throws an exception Commented Oct 1, 2019 at 23:54

1 Answer 1

1
const mongoose = require('mongoose');
const dbpath = "mongodb+srv://cluster0-xxxx.mongodb.net/Game";

mongoose.connect(dbpath, {user: 'username', pass: 'password'})
  .then(()=> console.log("Now connected to MongoDB!"))
  .catch(err=> console.error("Something went wrong", err));

There was an issue with Connection string. All sorted with username and password.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.