From f1cb95d88abaf547450929dfd7dfc8ec7956bc66 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Mon, 11 Sep 2017 18:29:16 -0600 Subject: [PATCH] Fixed tags route issue Error in GET /api/tags corrected. Database structure corrected, Tags model eliminated, and route correctly gathering unique tags from all articles. --- api/models/tag-model.ts | 8 -------- api/routes/tag-routes.ts | 30 +++++++++++++----------------- 2 files changed, 13 insertions(+), 25 deletions(-) delete mode 100644 api/models/tag-model.ts diff --git a/api/models/tag-model.ts b/api/models/tag-model.ts deleted file mode 100644 index f2f84e9..0000000 --- a/api/models/tag-model.ts +++ /dev/null @@ -1,8 +0,0 @@ - -import * as mongoose from 'mongoose'; - -const tagSchema = new mongoose.Schema({ - tags: [String] -}); - -export const Tag = mongoose.model('Tag', tagSchema ); diff --git a/api/routes/tag-routes.ts b/api/routes/tag-routes.ts index 794a6e0..09708a8 100644 --- a/api/routes/tag-routes.ts +++ b/api/routes/tag-routes.ts @@ -1,27 +1,23 @@ -import { Router } from 'express'; -import { Tag } from '../models/tag-model'; +import { Article } from '../models/article-model'; +import { Router, Request, Response, NextFunction } from 'express'; + const router: Router = Router(); -router.get('/', getTags); -function getTags(req, res) { +// FIXME: Rewrite to pull from Articles... +router.get('/', (req: Request, res: Response, next: NextFunction) => { - const projection = {tags: 1, _id: 0}; + Article + .find() + .distinct('tagList') + .then((tagsArray: [string]) => { + return res.json({tags: tagsArray}); + }) + .catch(next); - Tag - .find({}, projection) - .exec((err: any, results) => { +}); - if (err) { - console.log('Error finding tags'); - res.status(500).json(err); - } else { - console.log('Found ' + results.length + ' result(s)'); - res.status(200).json(results[0]); - } - }); -} export const TagRoutes: Router = router;