hello I am trying to update a document on mongodb but everytime I run the lambda function below it does not return the new document but returns a http response 200. I have tried to update the logic in so many forms but I get the response 200 but the update does not happen. The lambda function takes 2 path parameters i.e the userid and the date to find the specific plan then updates it... It neither updates nor returns the new document. Please help!
const Plan = require('../../model/planModel');
const User = require('../../model/userModel');
const { connectDB } = require('../../config/db');
const { verify, decode } = require('jsonwebtoken');
const { decode_token } = require('../../utils');
const { findByIdAndUpdate } = require('../../model/planModel');
exports.handler = async (event) => {
try {
await connectDB();
//console.log(event)
const { userid, date } = event.pathParameters;
const exists = await Plan.findOne(userid, date);
console.log('I made it here: ', exists);
const {
excitedAbout,
gratefulForMorning,
mainThreeTodo,
dailyToDo,
meals,
water,
exercise,
meditation,
relaxation,
happyScale,
happyMoment,
gratefulForTonight,
dailyQuestion,
} = event.body;
const updatedPlan = await Plan.findByIdAndUpdate(
{ _id: exists._id },
{
excitedAbout: excitedAbout,
gratefulForMorning: gratefulForMorning,
mainThreeTodo: mainThreeTodo,
dailyToDo: dailyToDo,
meals: meals,
water: water,
exercise: exercise,
meditation: meditation,
relaxation: relaxation,
happyScale: happyScale,
happyMoment: happyMoment,
gratefulForTonight: gratefulForTonight,
dailyQuestion: dailyQuestion,
},
{ new: true }
);
console.log('this is a plan', updatedPlan);
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(updatedPlan),
};
} catch (error) {
console.log(error);
}
};
This is the data I want to update:
{
"user": "62d1a251806d10095ff0f0a6",
"date": "2022-09-09T18:30:00.000Z",
"excitedAbout": "Excited About something",
"gratefulForMorning": "",
"mainThreeTodo": [
{
"text": "HELLO I survived",
"checked": false
},
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
}
],
"dailyToDo": [
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
}
],
"meals": {
"breakfast": [
{
"meal": "",
"calories": ""
}
],
"lunch": [
{
"meal": "",
"calories": ""
}
],
"dinner": [
{
"meal": "skuma",
"calories": ""
}
]
},
"water": 7,
"exercise": false,
"relaxation": false,
"meditation": false,
"happyScale": 0,
"happyMoment": "",
"gratefulForTonight": "",
"dailyQuestion": {
"question": "",
"answer": ""
}
}
and this is what I'm getting
{
"meals": {
"breakfast": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c3"
}
],
"lunch": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c2"
}
],
"dinner": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c1"
}
]
},
"dailyQuestion": {
"question": "",
"answer": ""
},
"_id": "631ba3e559b8a53d732a1958",
"user": "62d1a251806d10095ff0f0a6",
"date": "2022-09-09T18:30:00.000Z",
"excitedAbout": "",
"gratefulForMorning": "",
"mainThreeTodo": [
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bb"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bc"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bd"
}
],
"dailyToDo": [
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2be"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bf"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2c0"
}
],
"water": 0,
"exercise": false,
"meditation": false,
"relaxation": false,
"happyScale": 0,
"happyMoment": "",
"gratefulForTonight": "",
"createdAt": "2022-09-09T20:36:53.680Z",
"updatedAt": "2022-09-13T12:01:02.966Z",
"__v": 0
}
it's like it is never passed