Error creating an instance from a mongoose schema

Hi all,

I need some help, I’ve been stuck for too long now on the MongoDB and Mongoose - Create a Model challenge,
I really don’t understand what I’m doing wrong here, thanks a lot for your help :slight_smile:

This is my code:

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

const Schema = mongoose.Schema;

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

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

This is my glitch link to the challenge:
https://glitch.com/edit/#!/ambiguous-farmhouse?path=myApp.js:45:0

The Glitch url you post does not work.

Hi @RandellDawson
I just updated the link, hopefully it works now

Your code should work as long as you have a valid uri. I tested your code with a working URI and passes the test.

Click on the Get a Hint link under Get Help. There is some advice there that should help to make sure you have properly assigned the uri to the .env variable MONGO_URI.

Thanks @RandellDawson for looking into it, good to know that the code should work with a valid URI.

So, I tried again, created a new cluster, got a new URI , but it still doesn’t work.

My URI looks like this:

mongodb+srv://joe-messi:myPassword@cluster0-uu0xc.mongodb.net/test?retryWrites=true&w=majority

and the .env file looks like this:

MONGO_URI='mongodb+srv://joe-messi:myPassword@cluster0-uu0xc.mongodb.net/test?retryWrites=true&w=majority'

Any thoughts ?
thanks for the help

I literally have no idea what I’m doing wrong. I spent more than 7 hours now trying to fix this.
Is there something you have to do before the cluster will start to work ? or just copying the string provided will do ?

Did you allow any ip address (whitelist) to access the database per the introduction instructions?

yes I did, I spent a few hours chatting with someone from MongoDB Atlas and we realised that the connection URI is just fine, we created an instance of a Person and we saw it added to the collection of the cluster in the Atlas web site, so the connection fine, there must be another problem or something I’m missing that the freecodecamp validation doesn’t like, but I’m leaving it like that now, I will continue with the lessons and hopefully this won’t effect future validation.
Thanks for your help

@JoeMessi I am not sure what the problem is. When I remix your project and use my own URI, I can pass the challenge with the code you have written.

What is the exact url you are submitting for the Solution text box?

Do you see any errors in the browser console and/or the Glitch server console when you submit the url?

Hi @RandellDawson, apparently now when I submit the glitch url for submission everything is fine, I didn’t change anything from yesterday and I’m doing exactly the same thing I did yesterday, but now, this morning, it’s working, I don’t know what to say, I spent the whole day trying to fix something that wasn’t broken I guess, probably something to do with the browser ??? oh well, good that now it’s working and I can continue with the lessons in peace :slight_smile: thanks again for looking into this with me.