Skip to content

Commit

Permalink
Merge pull request processing#2772 from Mubashirshariq/Fix/Error-hand…
Browse files Browse the repository at this point in the history
…ling-in-user-controller

Fix/error handling in user controller.js
  • Loading branch information
lindapaiste authored Jan 4, 2024
2 parents 73d6234 + 68bc603 commit 45c2ce1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 19 additions & 5 deletions server/controllers/aws.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import S3Policy from 's3-policy-v4';
import s3 from '@auth0/s3';
import mongoose from 'mongoose';
import { getProjectsForUserId } from './project.controller';
import { findUserByUsername } from './user.controller';
import User from '../models/user';

const { ObjectId } = mongoose.Types;

Expand Down Expand Up @@ -237,10 +237,24 @@ export function listObjectsInS3ForUser(userId) {

export function listObjectsInS3ForUserRequestHandler(req, res) {
const { username } = req.user;
findUserByUsername(username, (user) => {
User.findByUsername(username, (err, user) => {
if (err) {
console.error('Error fetching user:', err.message);
res.status(500).json({ error: 'Failed to fetch user' });
return;
}
if (!user) {
res.status(404).json({ error: 'User not found' });
return;
}
const userId = user.id;
listObjectsInS3ForUser(userId).then((objects) => {
res.json(objects);
});
listObjectsInS3ForUser(userId)
.then((objects) => {
res.json(objects);
})
.catch((error) => {
console.error('Error listing objects in S3:', error.message);
res.status(500).json({ error: 'Failed to list objects in S3' });
});
});
}
6 changes: 0 additions & 6 deletions server/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ const random = (done) => {
});
};

export function findUserByUsername(username, cb) {
User.findByUsername(username, (err, user) => {
cb(user);
});
}

export function createUser(req, res, next) {
const { username, email } = req.body;
const { password } = req.body;
Expand Down

0 comments on commit 45c2ce1

Please sign in to comment.