Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.

express-workshop-all steps#5

Open
Behnazz wants to merge 1 commit intoCodeYourFuture:masterfrom
Behnazz:master
Open

express-workshop-all steps#5
Behnazz wants to merge 1 commit intoCodeYourFuture:masterfrom
Behnazz:master

Conversation

@Behnazz
Copy link
Copy Markdown

@Behnazz Behnazz commented Jul 17, 2018

No description provided.

Comment thread data/posts.json
{
"1467390356291": "This is my very first blog post!"
}
["[\"[\\\"[\\\\\\\"[\\\\\\\\\\\\\\\"[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"1467390356291\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"This is my very first blog post!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"1531861103496\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"Hi\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}]\\\\\\\\\\\\\\\",{\\\\\\\\\\\\\\\"1531861155200\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Hello\\\\\\\\\\\\\\\"}]\\\\\\\",{\\\\\\\"1531861738283\\\\\\\":\\\\\\\"check\\\\\\\"}]\\\",{\\\"1531861917411\\\":\\\"First\\\"}]\",{\"1531861944247\":\"second\"}]",{"1531861944627":"second"}] No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are there all these backslashes in the JSON?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should just be an array of objects

Comment thread server.js

app.post("/create-post", function (req, res) {
fs.readFile(__dirname + "/data/posts.json", function (error, file) {
const parsedFile = JSON.parse(file);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are turning the file contents into an object, why are you not adding the data to this object?

Comment thread server.js
fs.readFile(__dirname + "/data/posts.json", function (error, file) {
const parsedFile = JSON.parse(file);
var storage = [];
storage.push(file.toString());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why push the entire file as a string into the array?

Comment thread server.js
const parsedFile = JSON.parse(file);
var storage = [];
storage.push(file.toString());
storage.push({ [Date.now()]: req.fields["blogpost"] })
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not you have a array that contains a string representation of an array and another object. Not what you'd want.

Comment thread server.js
var storage = [];
storage.push(file.toString());
storage.push({ [Date.now()]: req.fields["blogpost"] })
fs.writeFile("./data/posts.json", JSON.stringify(storage), function (error) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you stringify this, you end up with the crazy contents in your file.
Make sure you have the correct object structure before writing it back into the file.

Copy link
Copy Markdown

@nennes nennes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the JSON parsing and also complete the Handlebars part.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants