Fixed non functioning code

This commit is contained in:
Alex Holliday
2024-07-12 10:24:46 -07:00
parent 7462458eca
commit 12e85aed03
3 changed files with 16 additions and 13 deletions

View File

@@ -50,11 +50,14 @@ export const getMonitorsByUserId = createAsyncThunk(
async (token, thunkApi) => {
const user = jwtDecode(token);
try {
const res = await axiosInstance.get("/monitors/user/" + user._id, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const res = await axiosInstance.get(
`/monitors/user/${user._id}?limit=25`,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
return res.data;
} catch (error) {
if (error.response && error.response.data) {

View File

@@ -293,19 +293,19 @@ const getMonitorById = async (req, res) => {
*/
const getMonitorsByUserId = async (req, res) => {
try {
const limit = req.body.limit;
const limit = req.query.limit;
const monitors = await Monitor.find({ userId: req.params.userId });
// Map each monitor to include its associated checks
const monitorsWithChecks = await Promise.all(
monitors.map(async (monitor) => {
if(limit) {
if (limit) {
// Checks are order oldest -> newest
const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: 1,
}).limit(limit);;
const checks = await Check.find({ monitorId: monitor._id })
.sort({
createdAt: 1,
})
.limit(limit);
return { ...monitor.toObject(), checks };
} else {
// Checks are order oldest -> newest
const checks = await Check.find({ monitorId: monitor._id }).sort({

View File

@@ -5,7 +5,7 @@ const Monitor = require("../models/Monitor");
router.get("/", monitorController.getAllMonitors);
router.get("/:monitorId", monitorController.getMonitorById);
router.get("/user/:userId?limit", monitorController.getMonitorsByUserId);
router.get("/user/:userId", monitorController.getMonitorsByUserId);
router.post("/", monitorController.createMonitor);
router.post(