Skip to content

Commit

Permalink
fix: message upload error
Browse files Browse the repository at this point in the history
  • Loading branch information
kcwww committed Nov 21, 2024
1 parent 28d46a6 commit d3193fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
27 changes: 22 additions & 5 deletions app/api/crystal/message/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type MessageReq = {
};

export const POST = async (req: NextRequest) => {
console.log('POST');
const {
user_id,
crystal_id,
Expand All @@ -90,12 +91,25 @@ export const POST = async (req: NextRequest) => {
is_deleted,
is_opend,
} = (await req.json()) as MessageReq;

console.log(
'POST',
user_id,
crystal_id,
decoration_id,
decoration_color,
content,
sender,
letter_color,
is_deleted,
is_opend
);
await connectToMongoDB();
console.log('POST', 'connected to mongoDB');

const sessionDB = await mongoose.startSession();
sessionDB.startTransaction();

console.log('POST', 'sessionDB started');
try {
// MongoDB에 연결

Expand All @@ -116,10 +130,11 @@ export const POST = async (req: NextRequest) => {
],
{ session: sessionDB }
);

console.log('POST', 'message created', message);
if (!message) throw new Error('Failed to create message');

const message_id = message[0]._id;
if (!message) throw new Error('Failed to create message');

await Crystal.findOneAndUpdate(
{ _id: crystal_id },
Expand All @@ -133,18 +148,20 @@ export const POST = async (req: NextRequest) => {
},
{ new: true, session: sessionDB }
);

console.log('POST', 'crystal updated');
await sessionDB.commitTransaction();

console.log('POST', 'sessionDB committed');
return NextResponse.json({
message: 'Message created',
message_id,
ok: true,
});
} catch (error) {
await sessionDB.abortTransaction();

console.error('Error creating message : ', error);

await sessionDB.abortTransaction();
console.log('POST', 'sessionDB aborted');
return NextResponse.json(
{ error: 'Failed to create message ' + error },
{ status: 500 }
Expand Down
1 change: 1 addition & 0 deletions shared/components/modals/MessageSubmitModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const MessageSubmitModal = () => {
console.error('Failed to send message', error);
toast.error('메세지 전송에 실패했습니다.');
onClose();
setIsSubmitting(false);
}
};

Expand Down

0 comments on commit d3193fe

Please sign in to comment.