Add solution to part one of the express workshop exercise#3
Add solution to part one of the express workshop exercise#3rajgthub wants to merge 4 commits intoCodeYourFuture:masterfrom
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.
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.
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.
you're already iterating through your array, why do you need a second loop?
server.js
Outdated
| const app = express() | ||
|
|
||
| //reading posts | ||
| let allposts = [] |
There was a problem hiding this comment.
you can combine the 2 lines in 1 and declare the variable as const
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.
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.
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.
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.
try to use const or let instead of var, as var ofter behaves in mysterious ways
There was a problem hiding this comment.
yes, I will do it. I did not realise as it was in the forked version of the code (original).
Here is my solution for the homework exercise. Please have a look at them. Thanks very!