If referencing Schemas makes you want to cry, read this…

A month and a half into coding bootcamp I’ve realized that its is going to be full of ups and downs. There is going to be topics that just make sense, and other that are just going to make you want to cry and pull your hair out. Specially if this is your first real attempt at becoming coding literate. For me one of those topics that made me want to cry was how to reference Schemas. More accurately how to create a new object of a Schema that is referenced in another Schema. I say this because not all the precess is a grueling as the creating of a new element, in other words, the code in your routes folder that you need in order for a POST method to work on a depending model. The first part is easy, when telling your schema what kind of information is going to be your reference item you give tell it that it will have an object id and give it a reference (basically something that you’ll use to call it later on).

Now to the crying part. Say you want to create and object of this model that is referenced in another one. First, you have to start a function that will contain all the elements of your response. It will probably say something like “createBlah”or “create1234”. Next, you’ll need to use a findOne function to find the object to which you wish to add reference to this new object. This would look something like this.

function createFoodItem (req, res){
 // retrieve restaurant
 db2.Restaurant.findOne({_id: req.params.restId}, function(err, restObject){

The findOne function needs a callback function inside of it, so name it something that is clear of what step you intend to take with it. Up next is when the it start to get complicated.

Now inside of this callback function you need to tell the computer what is your new object going to contain. For me this step is complicated because normally I feel like I have to be explicit with computer, but in this case (after just calling another object) hey is time to make that other object. It should look like this:

const newFood = db.Food({
 name: req.body.name,
 weight: req.body.weight,
 datePrepared: req.body.datePrepared
 });

By this point you need to understand you already have a new object with an id and everything. Now you need to save and push that object. Save the new reference object, and push it into the other object which is supposed to be referenced in. That consist of a save and it looks like this:

newFood.save(function(err, savedFood) {
 if (err) {
 res.status(501);
 console.log(‘Error saving food to DB.’, err);
 } else {
 console.log(“Saved food”);
 // update restaurant to include new food
 restObject.foodLeft.push(savedFood)

Now that we are done with that, no just kidding you still need to do other stuff. Lastly you need to save that first call back function from the start of this story, like this:

restObject.save(function (err, data){
 if (err) {
 console.log(“Messed up”);
 res.status(501);
 } else {
 console.log(“got it”);
 res.json(savedFood)
 }

After this last bit of pain, you are home free, of course remember to close all your braces and stuff. Check for typos before you freak out, and console log the shit out of it that way you can figure out faster where are you messing up. Referencing was very challenging for me because I kept thinking that stuff in a function happens from just top to bottom, and it doesn’t, specially not inside of a fun callback function like this. In this one you basically draw a way to grab the location that you need to create a new object and from there you save the whole path out. You need to dive into the data, place your model, and save your way out. I hope this helps someone. If not watch some videos. There is not a lot of good ones because is a difficult topic to explain, but there is a couple out there that will make you life easier.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.