import {Router} from 'express';
import { sample_exercises, sample_tags } from '../data';
import expressAsyncHandler from 'express-async-handler';
import { ExerciseModel } from '../models/excersise.model';
const router = Router();
//api for seeding the db
//http://localhost:5000/api/exercises/seed
router.get("/seed", expressAsyncHandler(
async (req, res) => {
//count variable to see if db has items
const exerciseCount = await ExerciseModel.countDocuments();
if(exerciseCount > 0 ){
res.send("Already seeded!");
return;
}
//otherwise create all the exercises in the db
await ExerciseModel.create(sample_exercises);
res.send("Seed is done!");
}
));
//using an async function directly results in inconsistent behavior
//hence the usage of express-async-handler
router.get("/", expressAsyncHandler(
async (req, res) => {
//calling find() without a parameter gets all the items from the db
const exercises = await ExerciseModel.find();
res.send(exercises);
}
));
router.get("/search/:searchTerm", expressAsyncHandler(
async (req, res) => {
//because we're searching through the db, we have to use
//regex per mongodb standards
//regex i to make the searchTerm case insensitive
const searchRegex = new RegExp(req.params.searchTerm, 'i');
//$regex: is the operator for regex's
const exercises = await ExerciseModel.find({name: {$regex:searchRegex}});
// const searchTerm = req.params.searchTerm;
// const exercises = sample_exercises
// .filter(exercise => exercise.name.toLowerCase()
// .includes(searchTerm.toLowerCase()));
res.send(exercises);
})
);
router.get("/:tagName", (req, res) =>{
const tagName = req.params.tagName;
const exercises = sample_exercises
.filter(exercise => exercise.targetgroup.includes(tagName));
res.send(exercises);
});
// router.get("/:tagName", expressAsyncHandler(
// async (req, res) => {
// const exercises = await ExerciseModel.find({tags: req.params.tagName})
// res.send(exercises);
// }
// ))
router.get("/:exerciseId", expressAsyncHandler(
async (req, res) => {
const exercise = await ExerciseModel.findById(req.params.exerciseId);
//const exercise = await ExerciseModel.find({_id: req.params.exerciseId})
res.send(exercise);
})
);
// router.get("/:exerciseId", (req, res) => {
// const exerciseId = req.params.exerciseId;
// const exercise = sample_exercises.find(exercise => exercise.id == exerciseId);
// res.send(exercise);
// });
export default router;
The get exerciseId is the only one that is giving me trouble, it some how only returns an empty array.
I’ve listed some more of my code for further insight.
import { Schema, model } from "mongoose";
export interface User {
id: string;
email: string;
//we don't save token in the db, hence the pw
password: string;
name: string;
address: string;
isAdmin: boolean;
}
export const UserSchema = new Schema<User>(
{
name: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type: String, required: true},
address: {type: String, required: true},
isAdmin: {type: Boolean, required: true},
},
{
timestamps: true,
toJSON:{
virtuals: true
},
toObject:{
virtuals:true
}
}
);
export const UserModel = model<User>('user', UserSchema);
Model looks like this
import dotenv from 'dotenv';
dotenv.config();
// for things like process.env.MONGO_URI
import express from "express";
import cors from "cors";
import exerciseRouter from './routers/exercise.router';
import userRouter from './routers/user.router';
import { dbConnect } from './configs/database.config';
import tagRouter from './routers/tag.router';
//each time we start the server, it will try to connect to the db
dbConnect();
const port = 5000;
const app = express();
app.use(express.json());
//use cors so that the credentials are the same
//i.e. use the same localhost so it's userfriendly (not only for developers)
app.use(cors({
credentials:true,
origin:["http://localhost:4200"]
}));
app.use("/api/exercises", exerciseRouter);
app.use("/api/users", userRouter);
app.use("/api/tags", tagRouter);
app.listen(port, () => {
console.log("Website served on http://localhost:" + port);
});
Server looks like this
export const sample_exercises: any[] = [
{
id:1,
name: 'Benchpress',
description: 'It involves lying on a bench and pressing weight upward using either a barbell or a pair of dumbbells. During a bench press, you lower the weight down to chest level and then press upwards while extending your arm',
targetgroup: ['mid-chest', 'lower-chest', 'triceps']
},
{
id:2,
name: 'Pull up',
description: 'A pull-up is an upper-body exercise that involves hanging from a pull-up bar by your hands with your palms facing away from you, and lifting your entire body up with your arm and back muscles until your chest touches the bar. The pull-up movement uses multiple muscles at once, making it a compound exercise.',
targetgroup: ['lats', 'traps']
},
{
id:3,
name: 'Squat',
description: 'Start with your body weight as you dial in your form. After you get used to it, begin to incorporate the dumbbells. Push your butt back as you squat down to keep the pressure off your knees.',
targetgroup: ['upper-legs', 'core']
},
{
id:4,
name: 'Chin up',
description: 'An exercise in which one hangs by the hands from a support (such as a horizontal bar) and pulls oneself up until the chin is level with the support. specifically : such an exercise done with the palms facing inward compare pull-up.',
targetgroup: ['lats', 'biceps']
},
{
id:5,
name: 'Incline benchpress',
description: 'It involves lying on an incline bench and pressing weight upward using either a barbell or a pair of dumbbells. During a bench press, you lower the weight down to chest level and then press upwards while extending your arm',
targetgroup: ['upper-chest', 'triceps']
},
{
id:6,
name: 'Seated twisting dumbell curls',
description: 'Sit on a bench, hold two dumbbells at your sides with palms facing each other. Use your bicep to curl the dumbbells up to your shoulders, twisting your palms to face your chest as you lift them. Slowly lower the dumbbells back down to your side and repeat.',
targetgroup: ['biceps', 'forearm']
},
{
id:7,
name: 'Dips',
description: 'Perform the dips with your hands behind your back on a chair or bench. Start with your feet on the ground. As you become stronger, place your feet up on another chair to add resistance.',
targetgroup: ['biceps', 'front-delt', 'chest']
},
{
id:7,
name: 'Shoulder press',
description: 'You can perform these either seated or standing. Hold the dumbbells at shoulder level and press them high over your head. Perform the same movement if using bands.',
targetgroup: ['triceps','front-delt', 'chest']
},
{
id:8,
name: 'Plank',
description: 'Get into a push-up position on your elbows instead of your hands, keep your body straight and hold the position for 30 seconds for each round. Rest 15 seconds and then perform another rep. Perform five reps.',
targetgroup: ['core']
},
]
export const sample_tags:any[] = [
{name: "mid-chest"},
{name: "lower-chest"},
{name: "triceps"},
{name: "lats"},
{name: "biceps"},
{name: "forearm"},
{name: "traps"},
{name: "upper-legs"},
{name: "front-delt"},
{name: "core"},
]
export const sample_users:any[] = [
{
name: "John Hoe",
email: "john@gmail.com",
password: "encrypted123",
address: "Chiraq",
isAdmin: true,
},
{
name: "Jane Dough",
email: "jane@gmail.com",
password: "secured456",
address: "Bagdad",
isAdmin: false,
},
]
Data looks like this, it’s all seeded inside mongodb
const BASE_URL = 'http://localhost:5000';
export const EXERCISES_URL = BASE_URL + '/api/exercises';
export const EXERCISES_TAGS_URL = EXERCISES_URL + '/tags';
export const EXERCISES_BY_SEARCH_URL = EXERCISES_URL + '/search/';
export const EXERCISES_BY_ID_URL = EXERCISES_URL + '/';
export const TAGS_URL = BASE_URL + '/api/tags';
export const TAGS_BY_NAME = TAGS_URL + '/';
export const USER_LOGIN_URL = BASE_URL + '/api/users/login';
export const USER_REGISTER_URL = BASE_URL + '/api/users/register';
And here are the URL’s