# 2003-GHP Cookie Jar: Cody's Cafe Walkthrough
Put your pending and outstanding questions here ๐
Make the question in an H2 tag by using '##'
Example:
## What is JavaScript?
## what happens if we fail pillars?
- [Ben answering]: We went over this yesterday during the Pillars Q+A. Do your best. The idea of pillars is to give us (the instructors and fellows) and idea of where you stand and what your blind spots are so we can try to give more targeted help.
## Can you please explain class v instance methods difference in use and syntax? (".prototype."?)
Documentation: รง
.prototype methods are methods that are specific to an instance.
- Use class when you want the method to apply to the whole model - findByIngredient is a class method because it has to do with looking through everything in the model.
- Use instance when it applies to a particular row/instance in the model - i.e. you might want to change the status of a particular to-do list item (this applies to one to-do, not the whole concept of a to-do, if that makes sense)
## BeforeSave vs BeforeValidate. Both can do samething only difference is the stage of cycle?
- Yup I think you have the right idea. https://sequelize.org/master/manual/hooks.html
- If you need something to happen before validation, use beforeValidate, if not, you can use beforeSave. In this case we only needed it before saving because the action of the method didn't affect validation
## What's the difference between res.send and res.json?
- res.json converts what you're sending into JSON format. This is useful because then if it's received universally readable by other programming languages. In contrast, res.send's content type is html by default
## (:coffeeId route) When do we use the if/else and when do we use a try catch? Are they the same?
- If/Else statements and Try/Catch statements are not the same thing. When you're dealing with asynchronous functions, you should use a Try/Catch statement. This is because you may encounter failures or errors when making the call (ex. a file might not exist or it may be corrupted or the database server may be offline). For that reason, it's very common for async operations to throw errors and we need to be prepared to properly handle those errors. Try/Catch is a standard Javascript Exception handling mechanism. If the try block hits an error, the catch block will be executed and passed the error object.
## Would the catch block with next(err) in the โ/:coffeeIdโ route be good enough to send the 404 or did we also need the if/else in the try block with the res.status(404).send()?
- The try catch only catches something if there's an error reaching into the database, so it won't be sent if the API request successfully makes it to the database but doesn't find what it's looking for.
## will we really be able to code this quickly
- No! The goal (esp) for pillars isn't to code this quickly. Don't panic if you find yourself spending WAY more time. Take the time to check documentation, console.log to make sure you understand what's happening, etc. We are absolutely doing a speed through right now, and we've all done this project before so have taken time to make all the mistakes ^.^
- [From Ben]: You've seen us all make mistakes in our demos/live code. We've seen this stuff before that's why we know what to expect (generally). In your (and all of our) coding journey, things will move slower in which we spelunk and debug more than anything. I've spent more waking hours debugging than coding and I'm sure most developers will agree.
## will we be writing any test spec on pillars?
- On pillars, you will be reading test specs. You should not be writing test specs on pillars. Please practice reading test specs :)
## when we define association, it doesn't need to be vice versa? For example, when we do
--we don't need to do belongsTo?
- It's implied. However, it's always good to write the other relationship because of magic methods. See your repo for extra resources on magic methods. Also linked [here](https://gist.github.com/jbracht/1778e93ced532b902fc49d70a743ffb8)
## For hooks, these methods operate on every instance that is added to the model?
- a great source of documentation for hooks is https://sequelize-guides.netlify.com/hooks/
- But, to answer your questions (if I understand it correctly): yes!
- [From Ben: this was answered in multiple places including the Sequelize Cookie Jar but copying and pasting below]
- Every Sequelize Model instance has a lifecyle of which they are created, updated and destroyed.
Think about after a simple JavaScript function runs, it gets put into the call stack, it designates some memory (execution context) to be able run whatever is in the function and then after it's done, it gets popped off the call stack and then the memory of it gets released.
There are a four lifecycle events that an instance can go through:
- Validate
- Create
- Destroy
- Update
And Sequelize has default methods that allows us to *hook* into these lifecycle events.
We can define some logic in these methods to be able to do something with our instance before or after we save/create it in our database.
For more information, check out Julissa's [Amazing Gist](https://gist.github.com/Julissa93/6a6d29874d34a801d603d2522645025f)
## Any chance we will get to see the
Sequelize checkpoint solution before 6 so we can ask if we don't understand any of the solution code? (preferably earlier though)
- [From Ben]: We will release it soon.
## What settings are you using to colorize the {} [] ()?
- [From Ben]: Which fellow was this directed to?
- Mika & Chole
- [From Chloe]: Hi! ! It's a VS Code extension called.... Bracket Pair Colorizer. It is life changing and I defintiely suggest you use it (props to Mika for finding it)!
## Is res.status(404).send() within an if/else statement SETTING the status to 404 or sending whatever's inside (or 404 if not?) if the stuff isn't found? I'm asking more about res.status().json(), what res.status is doing (adapted to your suggestion to send json)
I understand why you're confused. I would use .status() to send the 404 status.
- I would write it two ways:
- res.status(404).send('Pug not found')
OR
- const err = Error('Pug not found') // create an error
- err.status(404) // change the status of the error
- throw err
## Question from Zoom Chat on `async/await` in Sequelize Models
- It's redundant to do this as you are likely calling these functions from somewhere (such as an Express route or in this case, the test specs) that is already an `async` function, which means it's redundant and will take a performance hit.
## how do we know when to do res.send and when to do res.json?
- In regards to test specs, you'll see an `expect` statement expecting `Content Type` to be `/json/`
- My thought is: If there's ever a case where you're not 100% sure what the client side code is written in, or there's a polyglot of code types, res.json is a safe bet. If you're confident that the receiving end will be able to understand res.send (if you're the sole developer on the project) then res.send is a safe bet!
## So the fellows did some async in their routes, so we do need to do it in the routes but not elsewhere or was it just by habit that they were probably doing it?
- Async/awaits are needed whenever there's a call being made to the database. That's common in routes, where you're almost always searching the database for something, and not typically needed in 'model' files, where tables and methods are defined.
- The important thing to note is that you don't want to be redundant. When you have the async await on the routes, you don't need them on the models! But if you DON'T have it on the routes, you'll need it on the models. Which is why you needed to include them on the Seq. checkpoint.
## I am having trouble with [Op.contains:]... what is the right format?
- So you'll have the documentation: https://sequelize.org/master/manual/model-querying-basics.html
- first, you need to require Sequelize.Op, either with: `const Op = Sequelize.Op` OR `{ Op } = require("sequelize"); `
- then, you can use `[Op.contains]: [whatever]`
OR
- you can write it how we wrote it in the walkthrough, where you do it all in one line (because we already had `const Sequelize = require("sequelize")` this works):
```
Coffee.findByIngredient = function(ingredient) {
return this.findAll({
where: {
ingredients:{
[Sequelize.Op.contains]: [ingredient]
}
}
})
}
```
## How do we get GraceHopper shirts?