score:2
I was having a similar issue recently and haven't figured out the actual reason behind this, but as a workaround try telling mongoose explicitly that the expirationDate
-field was changed:
req.user.expirationDate = new Date(
req.user.expirationDate.setDate(req.user.expirationDate.getDate() + 30)
);
req.user.markModified('expirationDate');
await req.user.save();
EDIT:
Just debugged it again and I think the reason behind this behaviour is your default
value for expirationDate
. Try passing it the Date.now
function instead of immediately setting it to a new date:
expirationDate: {type: Date, default: Date.now},
This fixed it for me without having to use markModified()
.
score:0
Although we still have some unsolved issues on why the same set of codes works differently. I have decided not to deal with it for the moment. Here's my solution to the original problem: change the datatype from Date to String. Here's the new set of codes:
Creating User schema at the frontend:
const userSchema = new Schema(
{
profileId: String,
expirationDate: { type: String, default: new Date().toDateString() },
credits: { type: Number, default: 0 },
},
{ timestamps: { createdAt: "created_at" } }
);
Updating user at the backend to MongoDB Atlas::
d = new Date(req.user.expirationDate);
req.user.expirationDate = new Date(
d.setDate(d.getDate() + 30)
).toDateString();
req.user.credits += 1;
const user = await req.user.save();
console.log(typeof req.user.expirationDate);//Checking the datatype of "expirationDate"
Source: stackoverflow.com
Related Query
- Why wouldn’t mongoDB update the “Date” field of my user entry?
- Why setUser doesn't update the user state after api call?
- How can I update the correct field when the user can type either a username or an email?
- In React ES6, why does the input field lose focus after typing a character?
- How to dynamically update the value for any input field with one event handler function, using hooks
- ReactJS + Redux: Why isn't MongoDB saving data to the database even with correct API requests?
- Why does calling useState's setter with the same value subsequently trigger a component update even if the old state equals the new state?
- Why styles don't update when saving the files in Tailwind CSS JIT mode and I need to restart the server?
- Why is the registered user not Authenticated in Firebase? Why can't the user add products to the database Firebase?
- Why won't my controlled Input component in React update with the state?
- Why use the JavaScript spread operator to update an item in an array?
- How can I update the date placeholder option onMouseOver in react
- why my context doesn't update when the context value updates?
- React.js: How can I show the birthdate field till date in React?
- Why the Search Function for Random user api is not working?
- Why navbar components doesn't update after user logs in? (no Redux)
- Why React-router doesn't re-render the page and doesn't update the data when the url changes?
- Update color as the user scrolls on the array container
- Why won't it work this way to display what the user has clicked to appear in the modal?
- Auto update the user interface in ReactJS
- why does it not update the sql row in the react web application?
- Why React component is not update when the props passed to it changes?
- Why user is logging out after refresh the page? React/Redux app
- Display mongoDB data in a table with most recent date at the top of list in ReactJS
- How not to allow the user to enter a value in input field with a selection option
- want to group mongoDB documents by user. but after grouping, I don't want the user's mail field to appear again in the addresses. how?
- Why can't I update the state using Redux?
- React/JavaScript, can someone explain why setState doesn't seem to correctly update the state during the function?
- Why the interval callbacks belongs to first render is not capable of sending the update instruction(count +1) to React everytime the interval fires?
- Why a canvas update reload the page ? React
More Query from same tag
- How to reset the value of antd autocomplete, if the value is not in the data source
- Warning after signup and login in ReactJS
- how to use one component render many html fragment in reactjs?
- getting array of objects(nested) from api, looping 3x times , opinion, reactjs
- How to set the initial value for Mutlti Select in FormItem in ANTD
- React setState for multi level state object
- Some react Lottie Animations code-splitted with loadable crash on Gatsby development but work in prod
- Cannot assign to 'stockItems' because it is a read-only property. TS2540
- How to fix 'Invariant Violation' caused by cyclic dependencies in react-redux
- Don't loadOptions on initial render
- aws-amplify forgot password function with react js sending SMS instead of email
- How do 'nubs' work in Bot Framework React Webchat?
- React's Python-like formatted string
- how to set a default output message in react speech recognition?
- How to display a modal dialog box in ReactJS?
- Formik values not updating with state
- Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
- Problems with my routes in my production project with node js ERROR
- react-router, The prop `history` is undefined
- Tabs not loading pane information
- How to make redirect in nested routes with React Router
- How to use google fonts in a typescript react-app with JSS?
- Import 'GoogleMobileAds' not found in Ios (react native)
- looking for a way to pass the token to apollo-client via cookies not localstorage
- React state created with useState doesn't update automatically when my API changes
- React Formik - Validating Form on Component Re-Mounting and Enabling Button
- Error: `redirect` can not be returned from getStaticProps during prerendering
- Set button made in js to route to another page in React
- Passing Redux action to nested child components
- ReactJS - How to setSate in a map function