From 4ff5c19c9f3c4c9488d8cf70c64e045d5d01d6b8 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 16 Mar 2022 22:00:52 -0400 Subject: function to get posts --- public/views/createPost.html | 8 +++++-- public/views/dashboard.html | 52 +++++++++++++++++++++++++++++++++++++++++++- routes/posts.js | 17 ++++++++++----- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/public/views/createPost.html b/public/views/createPost.html index e01ad55..30c9cad 100644 --- a/public/views/createPost.html +++ b/public/views/createPost.html @@ -72,7 +72,7 @@

Create New Post

-
+
Error! Please fill out all fields.
- +
+
+ +
+
diff --git a/public/views/dashboard.html b/public/views/dashboard.html index 3791678..6556fd9 100644 --- a/public/views/dashboard.html +++ b/public/views/dashboard.html @@ -45,8 +45,8 @@
+
@@ -58,6 +58,56 @@ + \ No newline at end of file diff --git a/routes/posts.js b/routes/posts.js index d81327f..3ae3a3c 100644 --- a/routes/posts.js +++ b/routes/posts.js @@ -8,7 +8,6 @@ router.get("/posts", (req, res) => { let query = store.collection("posts"); query - .limit(1) .get() .then((querySnapshot) => { querySnapshot.forEach((documentSnapshot) => { @@ -23,18 +22,24 @@ router.get("/posts", (req, res) => { router.post("/new", (req, res) => { const { title, content, tags, publishDate } = req.body; const store = firebase.firestore(); + const id = store.collection("posts").doc().id; const post = { + id, title, content, tags: String(tags).split(",").length > 0 ? String(tags).split(",") : [], publishDate, }; let query = store.collection("posts"); - query.add(post).then(() => { - res.json({ success: true }); - }).catch((err) => { - res.json({ success: false, err }); - }); + query + .doc(id) + .set(post) + .then(() => { + res.json({ success: true }); + }) + .catch((err) => { + res.json({ success: false, err }); + }); }); module.exports = router; -- cgit v1.2.3