MongoDB and Mongoose - Create and Save a Record of a Model, user is not allowed to do action [insert] on [test.people]

Hi,

I have done the exercise exactly how I’m asked to but I keep getting this error, I already searched for it but no one seems to have posted about it.

MongoError: user is not allowed to do action [insert] on [test.people]
    at MessageStream.messageHandler (/home/user/Desktop/freeCodeCamp/Mongoose/boilerplate-mongomongoose/node_modules/mongodb/lib/cmap/connection.js:299:20)
    at MessageStream.emit (node:events:390:28)
    at processIncomingData (/home/user/Desktop/freeCodeCamp/Mongoose/boilerplate-mongomongoose/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/user/Desktop/freeCodeCamp/Mongoose/boilerplate-mongomongoose/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (node:internal/streams/writable:390:12)
    at _write (node:internal/streams/writable:331:10)
    at MessageStream.Writable.write (node:internal/streams/writable:335:10)
    at TLSSocket.ondata (node:internal/streams/readable:777:22)
    at TLSSocket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:234:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:199:23) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError'

My code:

require('dotenv').config();

/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connection successful!"))
.catch((err) => console.log("Connection SUCKS!", err.message));

//Create Schema
const { Schema } = mongoose;

let personSchema = new Schema({
  name: { type: String, required: true},
  age: Number,
  favoriteFoods: [String]
});

//Create Model
let Person = mongoose.model('Person', personSchema);

const createAndSavePerson = (done) => {
  let john = new Person({ name: 'John', age: 55, favoriteFoods: ['string1', 'string2'] });
  
  john.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data);
  });
};

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0

MongoDB and Mongoose - Create and Save a Record of a Model

Any solutions?

Thank you!

When you set up your Mongo Atlas database user, you did not give the user read and write permissions.

I was sure I did, is it possible to edit or do I need to restart? Project role is “Project Owner”

If you click on the Database Access link on the left menu, you should see a Database User Privileges section. Under Built-in Role, Read and write to any database should be selected.

2 Likes

That solved it, thank you very much!

I’m sure I followed every step but maybe I missed that one.