Zuko
July 27, 2023, 5:57am
1
I read the “Create Record” section in the second link I included but I am still confused about how to go about doing this. I understand I need to basically:
Create a model of a person, using the schema from exercise 2
Create a new person, including their attributes
Save the new person you created
Put your new person inside the createAndSavePerson function
But I just feel stuck on how to do it. I see the format in the example the problem gave and I also see the example in the second link, but I am still confused. Can someone help me?
By Nick Karnik Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in...
require('dotenv').config();
let mongoose = require('mongoose');
const mySecret = process.env['MONGO_URI']
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);;
const createAndSavePerson = (done) => {
done(null /*, 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;
are you saying you are not sure where exactly “those implementations” should go into?
Zuko:
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);;
its supposed to be in “createAndSavePerson” and not outside
there is a typo of two semicolon
lets refer to instructions
Then, call the method document.save() on the returned document instance
as you have “Person” document at hand now you need to call “save” method on it, which is an “async” method, so its expected that you would handle it that way
look exercise “save” function call method and use it that way
is there something specific you need help with, please ask away, happy learning
Zuko
July 28, 2023, 1:35am
3
require('dotenv').config();
let mongoose = require('mongoose');
const mySecret = process.env['MONGO_URI']
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);
const createAndSavePerson = new Person({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
createAndSavePerson.save((err) => {
if (err) {
console.error("Error saving person:", err);
} else {
console.log("Person successfully saved");
}
})
const createAndSavePerson = (done) => {
done(null /*, 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;
Thanks for the response! Gave it try but I am still doing something wrong
Zuko
July 28, 2023, 1:56am
4
require('dotenv').config();
let mongoose = require('mongoose');
const mySecret = process.env['MONGO_URI']
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);
const createAndSavePerson = (name, age, favoriteFoods, callback) => {
const createAndSavePerson = new Person({
name: 'Joe',
age: '26',
favoriteFoods: 'pizza'
});
}
newPerson.save((err, savedPerson) => {
if (err) {
return callback(err);
} else {
return callback(null, savedPerson);
}
});
const createAndSavePerson = (done) => {
done(null /*, 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;
second attempt didn’t work either, this problem got me so confused, I don’t want to just look at the answer though
Zuko:
const createAndSavePerson = (name, age, favoriteFoods, callback) => {
const createAndSavePerson = new Person({
name: 'Joe',
age: '26',
favoriteFoods: 'pizza'
});
}
newPerson.save((err, savedPerson) => {
if (err) {
return callback(err);
} else {
return callback(null, savedPerson);
}
});
const createAndSavePerson = (done) => {
done(null /*, data*/);
};
why are you using “createAndSavePerson” in this way, rather use “Person” schema into this given “handler function”
see there is another “handler function” with that same name, simply move your “schema and save” into that "handler function
once you moved you codes to that handler function, then make use of “done” when its a successful save action
see if that helps, happy learning