From 8438ce27cf4648a8264e043c8e6dceb457dd0698 Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Tue, 29 Oct 2019 22:25:38 -0400 Subject: [PATCH] Get user's posts works now --- functions/handlers/post.js | 16 ++++++++++------ functions/handlers/users.js | 1 - functions/index.js | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index dc88e45..83f9558 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -27,16 +27,20 @@ exports.putPost = (req, res) => { }; exports.getallPostsforUser = (req, res) => { - admin.firestore().collection('posts').where('userHandle', '==', req.userData.handle ).get() - .then((data) => { + var post_query = admin.firestore().collection("posts").where("userHandle", "==", req.user.handle); + post_query.get() + .then(function(myPosts) { let posts = []; - data.forEach(function(doc) { + myPosts.forEach(function(doc) { posts.push(doc.data()); }); return res.status(200).json(posts); }) - .catch((err) => { - console.error(err); - return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'}) + .then(function() { + res.status(200).send("Successfully retrieved all user's posts from database."); + return; }) + .catch(function(err) { + res.status(500).send("Failed to retrieve all user's posts from database.", err); + }); }; diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 203aa58..b455b02 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -1,5 +1,4 @@ /* eslint-disable promise/catch-or-return */ - const { admin, db } = require("../util/admin"); const config = require("../util/config"); const { validateUpdateProfileInfo } = require("../util/validator"); diff --git a/functions/index.js b/functions/index.js index 33ce166..dfb27f3 100644 --- a/functions/index.js +++ b/functions/index.js @@ -47,7 +47,7 @@ app.get("/user", fbAuth, getAuthenticatedUser); const { getallPostsforUser, putPost } = require("./handlers/post"); -app.get("/getallPostsforUser", getallPostsforUser); +app.get("/getallPostsforUser", fbAuth, getallPostsforUser); // Adds one post to the database app.post("/putPost", fbAuth, putPost);