-
-
Notifications
You must be signed in to change notification settings - Fork 93
Add solution to part one of the express workshop exercise #3
base: master
Are you sure you want to change the base?
Add solution to part one of the express workshop exercise #3
Conversation
public/script.js
Outdated
| var postText = document.createElement('p'); | ||
| var thumbnail = document.createElement('img'); | ||
| var postContainer = document.querySelector('.post-container'); | ||
| data.map(obj => { |
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.
obj is not a meaningful name, please go for something related to what is stored in this variable
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.
Yes, it should be referring to a single post. I have changed it.
public/script.js
Outdated
| var thumbnail = document.createElement('img'); | ||
| var postContainer = document.querySelector('.post-container'); | ||
| data.map(obj => { | ||
| for (var blogpost in obj) { |
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.
you're already iterating through your array, why do you need a second loop?
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.
Yes, it is unnecessary here.
server.js
Outdated
| const app = express() | ||
|
|
||
| //reading posts | ||
| let allposts = [] |
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.
you can combine the 2 lines in 1 and declare the variable as const
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.
Yes, I have changed.
public/script.js
Outdated
| res.json().then(function(json) { | ||
| addBlogpostsToPage(json); | ||
| document.querySelector("form").reset(); | ||
| window.location.href = "http://localhost:3000/posts" |
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.
try to use a path instead of the full url. this way it would work both on your local computer and in the production server.
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.
That is a great idea! thanks for pointing out.
public/script.js
Outdated
| }); | ||
| }) | ||
| .catch(function (err) { | ||
| console.error(err) |
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 a real application you'd display some error where the user can see it.
maybe you could populate some text in a div that has error CSS styling, and make it visible.
| var postContainer = document.querySelector('.post-container'); | ||
| data.map(post => { | ||
| if (post.hasOwnProperty("blogpost")) { | ||
| var postDiv = document.createElement("div"); |
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.
try to use const or let instead of var, as var ofter behaves in mysterious ways
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.
yes, I will do it. I did not realise as it was in the forked version of the code (original).
nennes
left a comment
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.
good stuff! 👍
Here is my solution for the homework exercise. Please have a look at them. Thanks very!