A M
A M

Reputation: 116

Mongo db data insertion issue

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.

Upvotes: 1

Views: 73

Answers (1)

A M
A M

Reputation: 116

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.

Upvotes: 1

Related Questions