Skip to content

Commit

Permalink
add error handling if no data found for session_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Kylie Pace committed Jan 18, 2021
1 parent ec0ad84 commit 829983b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/repositories/MongoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class MongoRepository {
MongoClient.disconnect();
}

async findOne<T>(query: FilterQuery<T>, options?: FindOneOptions<T extends any ? any : T>): Promise<T | null> {
async findOne<T>(query: FilterQuery<T>, options?: FindOneOptions<T extends any ? any : T>): Promise<any | null> {
const collection = await this.collection()
return collection.findOne(
query,
Expand Down
6 changes: 4 additions & 2 deletions src/server/controllers/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export default async function(req: Request, res: Response){

try{
const results = await databaseService.getData(session_id);
console.log(results)
return res.status(200).json(results);
console.log(`results found for ${session_id}: `, results)
return results ?
res.status(200).json(results) :
res.status(404).send(`no data found for session_id ${session_id}`)
}
catch(err){
return res.status(500).send(err.message);
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/DatabaseService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ITransformedData from "../../interfaces/ITransformedData";
import MongoRepository from "../../repositories/MongoRepository";


Expand All @@ -8,8 +9,7 @@ export default class TransformService {
this.database = new DatabaseRepository();
}

async getData(sessionId: string){
console.log('session id: ', sessionId);
async getData(sessionId: string): Promise<ITransformedData | null >{
const query = { session_id: sessionId };
const results = await this.database.findOne(query);
return results;
Expand Down

0 comments on commit 829983b

Please sign in to comment.