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

Tell us what’s happening:
I was having a problem following some of the other posts on this issue, so I created this new one to try to make sense of what I’m doing wrong specifically. I even went through the documentation and ran my file locally, but I got the same error:

// running tests
Creating and saving a db item should succeed (Test timed out)
// tests completed

I’ve gone back to Replit because it really seemed like a hassle to keep using the command line, and Replit appears to be the lesser of two evils. But, honestly… what do I know. I’m still trying to pass.

I had already created my database on MongoDB. That url (along with the password) is what I used to create mySecret. When I tried running the file locally, I put that url in single quotes in the sample.env file. None of this is working.

I want to know if I’m having a code issue or a database connection issue. Those are my only 2 options … right? I have no more ideas at this point.

Can someone please explain to me (as if I’m a child) what I’m doing wrong? Thanks.

const mongoose=require('mongoose');
require('dotenv').config();


const mySecret = process.env['MONGO_URI']
mongoose.connect(mySecret);

const Schema = mongoose.Schema;

const personSchema = new Schema({
  name: { type: String, required: true },
  age: Number,
  favoriteFoods: [String]
});
const Person = mongoose.model('Person', personSchema);

function createAndSavePerson(done) {
    const mickeyMouse = new Person({ name: "Mickey Mouse", age: 200, favoriteFoods: ["pizza", "spaghetti", "hotdogs", "pineapple"] });

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

Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

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

Link to the challenge:

Is it possible that the following deprecation message has something to do with my problem:
Screen Shot 2023-01-22 at 2.15.21 PM

A couple of challenges previously, you were given these instructions for setting up your database connection:

mongoose.connect(<Your URI>, { useNewUrlParser: true, useUnifiedTopology: true });

Your issue is that you have not included useNewUrlParser and so the repl is crashing and throwing that deprecation warning at you.
Amend your connection statement and the challenge should pass.

1 Like

Thank you so much!! This fixed my problem.

1 Like

On a totally unrelated note (noting your profile pic), I’m branching out into the world of programming but I’m also a professional cellist.

1 Like

That’s so cool. The cello is an awesome instrument. If you don’t mind me asking … are you currently playing or teaching anywhere?

1 Like

I’m based in the UK and freelance with orchestras and whatnot. I don’t teach - I’m really bad at it!

1 Like