npm install node-cron
const cron = require("node-cron"); const User = require("../models/User"); // Assume `User` is your Mongoose model exports.deleteProfile = async (req,res) => { try{ const {userId} = req.body; //validate input if(!userId){ return res.status(400).json({success:false, message: "User ID is required" }); } // Find user to ensure existence const user = await User.findById(userId); if (!user) { return res.status(404).json({ success: false, message: "User not found" }); } // Schedule deletion after 5 days const deletionDate = new Date(); deletionDate.setDate(deletionDate.getDate()+5); cron.schedule(deletionDate.toISOString(), async () => { await User.findByIdAndDelete(userId); console.log(`User with ID ${userId} deleted successfully.`); }); return res.status(200).json({ success: true, message: `User deletion scheduled for ${deletionDate.toISOString()}`, }); } catch(error){ console.error("Error scheduling deletion:", error); return res.status(500).json({ success: false, message: "Internal Server Error" }); }
以上是使用 Node.js 安排刪除過程的一種方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!