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: