Create and save a record of a model-Mongodb and Mongoose exercise 3

Hi all, I’m having problems completing this challenge and I’ve been stuck on it for a while.

I’ve gone through the installation guide for mongodb, and tried a few different things, but I don’t really know what the problem is. From the error message it seems like it’s not authorizing the access, but I don’t know why.

The ip address under network access is set to 0.0.0.0/0.
I tried changing the password for the database, but that didn’t help.

This is the error message the console on replit gives when I try to submit the link on fcc

 npm start

> fcc-mongo-mongoose-challenges@0.0.1 start /home/runner/boilerplate-mongomongoose
> node server.js

(node:564) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Your app is listening on port 3000
GET
MongoError: user is not allowed to do action [insert] on [fcc-mongodb-and-mongoose.people]
    at /home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/pool.js:581:63
    at authenticateStragglers (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/pool.js:504:16)
    at Connection.messageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/pool.js:540:5)
    at emitMessageHandler (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/connection.js:310:10)
    at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/connection/connection.js:453:17)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  errmsg: 'user is not allowed to do action [insert] on [fcc-mongodb-and-mongoose.people]',
  code: 8000,
  codeName: 'AtlasError',
  [Symbol(mongoErrorContextSymbol)]: {}
}

This is the MONGO_URI secret environment variable (I copy/pasted from the mongodb connect to application link, and added the password and database name)

mongodb+srv://first-user2:passwordhere@cluster0.wvh0vkk.mongodb.net/fcc-mongodb-and-mongoose?retryWrites=true&w=majority

I wasn’t really sure what to do about the database name, and the only mention of it is right here in the environment variable. I created a collection named that in the cluster, but I’m not sure that’s right.

This is the code I added in the myApp.js file

mongoose.connect(process.env.MONGO_URI)
 
let personSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  age: {
    type: Number
  },
  favoriteFoods: [{
    type: String
  }]
})

let Person = mongoose.model("Person", personSchema);

const createAndSavePerson = (done) => {
  let bobo = new Person({name: "Bobo", age: 98, favoriteFoods: ["Pizza", "Laundry"]})

  bobo.save((err, data) => {
    if (err) return console.error(err)
    done(null, data);
  })
  
};

If anyone can help that would be great!

Check if this user has read and write permissions for your database on MongoDB Atlas, your error message seems to indicate that’s not the case.

1 Like

the default name for the database is Cluster0

1 Like

Okay, I checked that, but it says the role for first-user2 has read and write permissions for any database.

1 Like

This fixed it-thank you! I just switched cluster0 for fcc-mongodb-and-mongoose in the environment variable.

But I don’t really understand-I thought I was supposed to pick a name for the database. I created a database within the collection with my title-should I have changed the name of the collection instead?

I also have trouble understanding this, to be honest; I don’t see any issue with the connection string you provided.

Collections live within databases, not the other way around. The database is created first (upon connection, if it doesn’t already exist) and then the collection (upon insert, if it doesn’t already exist).

1 Like

Oh, okay-so in this case the database is cluster0 and what I was doing was making a collection. That makes sense, it added the people collection when the tests inserted something.
Thanks!

The issue was what ghulamshabirbaloch suggested. I had entered the wrong name for the database in the string instead of the actual default name, which is cluster0.

Normally you should be able to enter the default name of your choosing for the database in your connection string and mongoDB will create the database for you on the fly if it doesn’t already exist. The same goes for collections: as you noticed when you tried to insert a new Person, mongoDB (mongoose) created a new ‘people’ collection on the fly for you using the plural of the model name.

I don’t know why in your particular case passing the default database name doesn’t work (again, there’s nothing wrong with your connection string), but it is possible. For now, it’s enough that your connection works.

1 Like

Ok, just for anyone who might be getting this error later on try this:
Update your user permissions to readWriteAnyDatabase like this, go to Database access under the security and click the edit button on the right hand side of your user. Then under Database User Privileges click the drop down called, Built In Role, make sure the Read and Write to Any Database is selected, then finish by clicking the green update user at the bottom…After I changed this the test started working– see if it works for you.

My URI looked like this if you were wondering:
mongodb+srv://thabo_jen:bH0jGY1841rNpR@thabojen.psqbhf4.mongodb.net/?retryWrites=true&w=majority

I’m doing this in 2022, and it seems that the only input that needed to be added from MongoDB was the <password>, the rest of the link should remain the same.

Thank you thabocalebjen!!