-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Insert data #26
Insert data #26
Conversation
Can this be merged and closed? Looks like it addresses the below issue:
Cheers! |
@VirtualDOMinic I can't run the code right now, but from a basic inspection of the code it looks ok to me. If you pull and run it and it's all good then feel free to merge 👍 |
- `getData` is asynchronous, so `response.end` should be inside it, so it doesn't run before the data comes back from the database request (same as an API request). | ||
|
||
7. Navigate to `http://localhost:3000/dynamic` to check it's worked. | ||
|
||
|
||
- Now, let's write another function that inserts data into the database. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be a 'Step 6' right?
text: `INSERT INTO superheroes (name, superPower, weight) VALUES ($1, $2, $3)`, | ||
values: [name, superPower, weight] | ||
}; | ||
dbConnection.query(secured, (err, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does passing this secured
object work?
I would usually do dbConnection.query(text, values, (err, res) => { })
as per the docs: https://node-postgres.com/features/queries#parameterized-query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you can pass a query config object with text
and value
properties. That might be confusing to students who have only ever seen values passed a the second argument though
module.exports = getData; | ||
const insertData = (superHeroInfo, cb) => { | ||
const { name, superPower, weight } = superHeroInfo; | ||
dbConnection.query(`INSERT INTO superheroes (name, superPower, weight) VALUES (${name}, ${superPower}, ${weight})`, (err, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the solution should be the 'safe' version I think
|
||
1. Inside `src/dynamic.js`, write an asynchronous function `insertData` that takes a superhero information and a callback, and returns the callback function: | ||
```js | ||
const insertData = (superHeroInfo, cb) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There maybe needs to be some more explanation as to when this new function would be actually be called (i.e. in the handler for some post request, after the user fills out some form). And where the superHeroInfo
comes from.
I guess it would expand the workshop too much to include actual functionality for making and handling a POST request, so I think there at least needs to be an explanation of why we are writing this function.
Relates: #25
possible future edits: