Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Insert data #26

wants to merge 2 commits into from

Conversation

sultanassi95
Copy link

@sultanassi95 sultanassi95 commented Jan 31, 2018

Relates: #25

  • Added async insertData function instructions at the end of the code-along.
  • Mentioned postgres security concerns

possible future edits:

  • replace getData with the new function insertData, and mention a select query tip.

@sultanassi95 sultanassi95 requested review from macintoshhelper, a user and ZooeyMiller and removed request for macintoshhelper March 17, 2018 18:05
@VirtualDOMinic
Copy link

Can this be merged and closed?
@ZooeyMiller @macintoshhelper @ConchobarMacNessa

Looks like it addresses the below issue:

References #25

Cheers!

@ZooeyMiller
Copy link
Contributor

@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.
Copy link
Contributor

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) => {
Copy link
Contributor

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

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) => {
Copy link
Contributor

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) => {
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants