Creating a record of a model not working (MongoDB/Mongoose)

Link to the challenge https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/create-and-save-a-record-of-a-model

This is not passing the test for some reason. My code:

require('dotenv').config();
const mongoose = require('mongoose');
const { Schema } = require('mongoose');

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

const Person = mongoose.model('Person', personSchema)

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });


const createAndSavePerson = (done) => {
  const person = new Person({
    "name": "Qaqli",
    "age": 25,
    "favoriteFoods": ["doner", "laxmacun"]
  })
  person.save(function(err, data) {
    if (err) {
      done(err)
      return
    }
    console.log(data)
    done(null, data)
  })
}; 

Replit link boilerplate-mongomongoose (2) - Replit

Yes, it says
MongoError: user is not allowed to do action [insert] on [test.people]
But sometimes there’s no error and it just gets stuck on Listening on port 3000

Now something weird happened. I updated the role of my user in Database Access to atlasAdmin and this code which I got from another post worked, (it wasn’t working before)

var createAndSavePerson = (done) => {
  var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["eggs", "fish", "fresh fruit"]});

  janeFonda.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data);
  });
}; 

But mine, which now console logs the data, still doesn’t pass the test.

UPDATE: nvm, mine passed too now. Thanks.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.