Sorting blogpost by last modificated time added for /user endpoint

This commit is contained in:
2022-05-31 15:56:47 -03:00
parent 1320d62ad0
commit f436a01805
2 changed files with 5 additions and 3 deletions

View File

@@ -49,13 +49,12 @@ router.post("/", async (req, res) => {
userData.posts = [] userData.posts = []
try { try {
const postsReference = await db.collection('users').doc(userData.id).collection("posts").get() const postsReference = await db.collection('users').doc(userData.id).collection("posts").get()
postsReference.forEach(doc => { postsReference.forEach(doc => {
userData.posts.push({ userData.posts.push({
id: doc.id, id: doc.id,
data: doc.data() ...doc.data()
}) })
}) })
} catch (error) { } catch (error) {
@@ -63,6 +62,9 @@ router.post("/", async (req, res) => {
res.status(400).json(err) res.status(400).json(err)
} }
//Order posts by last modificated time
userData.posts.sort((a, b) => b.date.seconds-a.date.seconds)
//Hide user id //Hide user id
userData.id = null userData.id = null

View File

@@ -4,7 +4,7 @@ require('dotenv').config();
const express = require("express"); const express = require("express");
const server = express() const server = express()
const port = process.env.PORT || 3000 const port = process.env.PORT || 3001
//Accept with JSON files //Accept with JSON files
server.use(express.json()) server.use(express.json())