Skip to content
Snippets Groups Projects
Commit 07d56c31 authored by Jinyeong Kim's avatar Jinyeong Kim
Browse files

fix add plan

parent 48f9c3ae
No related branches found
No related tags found
No related merge requests found
......@@ -105,25 +105,45 @@ const addCollaborator = async (req, res) => {
// 하루 계획 추가
const addDayPlan = async (req, res) => {
try {
const { dayKey, places, route } = req.body;
const trip = await Trip.findById(req.params.id);
const { dayKey, places, route } = req.body; // 요청 데이터
const trip = await Trip.findById(req.params.id); // Trip 찾기
if (!trip) {
return res.status(404).json({ message: 'Trip not found' });
}
if (!trip.plans) {
trip.plans = new Map();
// `plans` 디버깅
console.log('Existing Plans:', JSON.stringify(trip.plans, null, 2));
// `Map`을 사용하여 dayKey 접근
const currentDay = trip.plans.get(dayKey);
if (!currentDay) {
return res.status(404).json({ message: `Day ${dayKey} not found in trip plans` });
}
trip.plans.set(dayKey, { places, route });
// 데이터 업데이트
if (places) currentDay.places = places;
if (route) currentDay.route = route;
// `Map`에 업데이트된 day 데이터 설정
trip.plans.set(dayKey, currentDay);
// Mongoose에 변경 사항 알림
trip.markModified('plans');
console.log('Before Save:', JSON.stringify(trip.plans.get(dayKey), null, 2));
// 변경 사항 저장
const updatedTrip = await trip.save();
res.status(200).json(updatedTrip);
console.log('After Save:', JSON.stringify(updatedTrip.plans.get(dayKey), null, 2));
res.status(200).json(updatedTrip.plans.get(dayKey)); // 수정된 day 데이터 반환
} catch (err) {
console.error('Error:', err);
console.error('Error updating day:', err);
res.status(500).json({ error: err.message });
}
};
module.exports = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment