I am working on an API for a next.js application that was deployed on Vercel. The API is using MongoDB alias for database and deployed on Heroku. The API has an auction model which allow users to create auction and set expiry date for their auction. I used node crone to check the Auction model to find auctions created by users, compare their expiration date with current date and if the expiration date is less than the current date, the auction would be marked as expired. This is the node crone codes below:
const autoUpdateDatabase = async () =>{
cron.schedule('* * * * *', async ()=>{
auctionToUpate = await Auction.find({isClose: false});
try{
auctionToUpate.map(async (single) =>{
await Auction.updateMany({_id: single._id}, { $currentDate: {currentDate: true}}, {new: true});
console.log('Current date updated for Auction')
if(single.expiryDate <= single.currentDate && single.isClose === false){
await Auction.updateMany({_id: single._id}, {
isClose: true,
expiryDate: null
}, {new: true});
return console.log('Update carried out for Auction current time');
}
});
}catch(err){
console.log(err);
};
return console.log('Nothing to update for Auction');
});
};
The node crone runs every one minutes. The challenge I am having now is to go about this. Take for instance, if I wanted my auction to expire in 30 minutes and I set the expiration date and time to be 2022-09-12T13:00, my expiration date and time would be saved this way in mongoDB: 2022-09-12T13:00. If for example, the current date is still 2022-09-12T13:00, which is generated via node crone is saved this way on mongoDB: 2022-09-12T12:00. I don't know why the current date and time generated via my server is different from the date and time generated via the client if for example both date and time were set the same time. How do I make the server current date generated via my server to be saved without mongoDB subtracting an hour from it? Is there a way around this?