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.
This commit is contained in:
Josh Black 2017-09-11 18:29:16 -06:00
parent 58c382ea8a
commit f1cb95d88a
2 changed files with 13 additions and 25 deletions

View File

@ -1,8 +0,0 @@
import * as mongoose from 'mongoose';
const tagSchema = new mongoose.Schema({
tags: [String]
});
export const Tag = mongoose.model('Tag', tagSchema );

View File

@ -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;