I have literaly done everything.I have checked for the error also and is it telling me that new instance is saved in the database (in terminal). But still challenge is not complete it is displaying me this error:Failed:Creating an instance from a mongoose schema should succeed
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Challenge: MongoDB and Mongoose - Create a Model
Link to the challenge:
We need to see your code otherwise we can’t help. Post your Replit or a repo.
1 Like
require('dotenv').config();
const mongoose = require('mongoose');
const url = process.env.MONGO_URI;
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Connected to MongoDB');
})
.catch(err => {
console.error('Error connecting to MongoDB:', err);
});
let Person = require("./personSchema"); // Import the Person model
const createAndSavePerson = (done) => {
const newPerson = new Person({
name: "John Doe",
age: 24,
favouriteFoods: ["Pizza", "Burger"]
});
newPerson.save((err, data) => {
if (err) return done(err);
done(null, data);
});
};
createAndSavePerson((err, data) => {
if (err) {
console.error("Error:", err);
} else {
console.log("Saved person:", data);
}
});
const createManyPeople = (arrayOfPeople, done) => {
done(null /*, data*/);
};
const findPeopleByName = (personName, done) => {
done(null /*, data*/);
};
const findOneByFood = (food, done) => {
done(null /*, data*/);
};
const findPersonById = (personId, done) => {
done(null /*, data*/);
};
const findEditThenSave = (personId, done) => {
const foodToAdd = "hamburger";
done(null /*, data*/);
};
const findAndUpdate = (personName, done) => {
const ageToSet = 20;
done(null /*, data*/);
};
const removeById = (personId, done) => {
done(null /*, data*/);
};
const removeManyPeople = (done) => {
const nameToRemove = "Mary";
done(null /*, data*/);
};
const queryChain = (done) => {
const foodToSearch = "burrito";
done(null /*, data*/);
};
/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
*/
//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------
exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;
personSchema.js:
const mongoose = require('mongoose');
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favouriteFoods: [String]
});
const Person = mongoose.model('Person', personSchema);
module.exports = Person;
Thanks for Replying ,
this is my full code
You are more likely to get help if you give people something they can more easily test.
My best guess:
A favoriteFoods
field of type [String]
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favouriteFoods: [String]
});
1 Like
Man you are genius!! I haven’t thought about it and it worked. For the past 3 days I was stuck with it and here it was just a small spelling mistake!!
1 Like
system
Closed
February 27, 2024, 7:29am
8
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.