Skip to content

Commit

Permalink
Pass comments to backends, and save in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
yudetamago committed Sep 10, 2021
1 parent 968b9dc commit 6827415
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 72 deletions.
21 changes: 2 additions & 19 deletions src/client/js/comment.js
@@ -1,18 +1,13 @@
// const form = document.getElementById("commentForm");
let forms = document.querySelectorAll("form");

const test = (event) => {
event.preventDefault();
console.log(event.target, "<- event target");
// console.log(form, "inside of test func");

try {
const input = event.target.querySelector("input");
let date = input.id;
let value = input.value;

console.log(date, value);

fetch("/", { // Fetch failed loading: POST "http://localhost:3000/"
method: "POST",
headers: {
Expand All @@ -27,20 +22,8 @@ const test = (event) => {
};

if (forms) {
// forms.addEventListener("submit", test);

console.log(forms);

var inputs;
var tempResult = {};
var result = [];

// console.log(forms[3][0].id); // 2021/08/28

for (var i = 0; i < forms.length; i++) {
for (let i = 0; i < forms.length; i++) {
const form = forms[i];
console.log(form);
form.addEventListener("submit", test);
console.log("Added!", i);
}
};
};
66 changes: 13 additions & 53 deletions src/controller.js
Expand Up @@ -46,9 +46,7 @@ export const getData = async (req, res) => {
finalResult = result;
result = [];


return finalResult;
// return res.render("home", { finalResult });

} catch(e) {
console.log(e);
Expand All @@ -64,79 +62,41 @@ export const saveData = async (req, res) => {
data = await getData();

for (var i = 0; i < data.length; i++) {
// console.log(data[i].date);

try {
const exist = await Article.findOne({ date: data[i].date})

if(exist) {
// console.log(`${data[i]} already exist`);
} else {
if(!exist) {
await Article.create(data[i]);
// console.log(`${data[i]} added`);
};

} catch(e) {
console.log(e);
// return res.end();
};

};
// console.log(data);
const dataInDB = await Article.find({});
return res.render("home", { data: dataInDB });
})();

/*if (data.length === 0) {
setTimeout(function() {
console.log("Wait for data coming");
}, 4000)
} else {
console.log("Data is ready");
}*/

if(data) {
console.log("Data is ready");
};
};

export const saveComment = async (req, res) => {
/*
try {
// console.log(req.body);
const { date, value } = req.body;
console.log(date);
setTimeout(function() {
console.log("wait for data found");
}, 5000);
const oneArticle = await Article.findOne({ date });
setTimeout(function() {
console.log("wait for data found again");
}, 5000);
console.log(`This is an article read from DB: ${oneArticle}`);
const updatedArticle = await oneArticle.updateOne( { clickedDate: value } );
if (!updatedArticle) {
console.log("Article doesn't exist");
return res.redirect("/");
}
try {
const { date, value } = req.body;
const oneArticle = await Article.findOne({ date });

console.log(`This is an article updated to DB: ${updatedArticle}`);
const updatedArticle = await oneArticle.updateOne( { clickedDate: value } );
if (!updatedArticle) {
console.log("Article doesn't exist");
return res.redirect("/");
} catch (e) {
return res.render("home", { err: e.message });
};
*/

console.log(req.body);
res.end();
}

return res.redirect("/");

} catch (e) {
return res.render("home", { err: e.message });
};
};

0 comments on commit 6827415

Please sign in to comment.