Skip to content

Instantly share code, notes, and snippets.

@WoolDoughnut310
Created October 28, 2022 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WoolDoughnut310/49d4cc3df65886437b79c0d714db9cf3 to your computer and use it in GitHub Desktop.
Save WoolDoughnut310/49d4cc3df65886437b79c0d714db9cf3 to your computer and use it in GitHub Desktop.
import { Response } from "express";
import { Filter, ObjectId } from "mongodb";
import getDb from "../db";
import { Song } from "../db/types";
import { Request } from "../types";
import asyncHandler from "express-async-handler";
...
router.get(
"/",
asyncHandler(async (req: Request, res: Response) => {
const db = getDb();
const collection = db.collection<Song>("song");
let filter: Filter<Song> = {};
if (typeof req.query.user === "string") {
filter = { userId: new ObjectId(req.query.user) };
}
const songs = await collection
.find(filter)
.sort({ date: -1 })
.toArray();
res.status(200).json(songs);
})
);
export default router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment