Hello All,
I am willing to pass the dates data from my MongoDB to the antD Date Picker as disabled dates but after fetching the data of date i get either undefined or null.
What is the approach here?
Here is my code:
The data in my db is:
{
"_id": {
"$oid": "63690afb0623f513b3f22704"
},
"userId": "63614c627330d358d7ceff2d",
"name": "SampleName",
"title": "Doctor",
"type": "Lab Consultation",
"description": "Help Me Please",
"timings": "2022-11-07T06:00:00.000Z",
"date": "2022-11-15T00:00:00.000Z",
"status": "pending",
"createdAt": {
"$date": {
"$numberLong": "1667828475559"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1667828475559"
}
},
"__v": 0
}
Schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const consultationSchema = new mongoose.Schema(
{
createdBy: {
type: mongoose.ObjectId,
ref: "userModel",
},
userId: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
type: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
timings: {
type: String,
required: true,
},
date: {
type: String,
required: true,
},
status: {
type: String,
required: false,
default: "pending",
},
},
{
timestamps: true,
}
);
const consultationModel = mongoose.model("consultations", consultationSchema);
module.exports = consultationModel;
Router Get Data:
router.get("/get-all-dates", authMiddleware, async (req, res) => {
try {
const consultation = await Consultation.find({
date: { $gte: "2022-11-11T00:00:00.000Z" },
});
const dates = consultation.date;
res.status(200).send({
message: "All Dates Fetched Successfully",
success: true,
data: dates,
});
console.log(dates);
} catch (error) {
console.log(error);
res.status(500).send({
message: "Fetching Dates Failed",
success: false,
error,
});
}
});
API Call in React:
const getDateData = async () => {
try {
dispatch(showLoading());
const response = await axios.get("/api/user/get-all-dates", {
headers: {
Authorization: "Bearer " + localStorage.getItem("token"),
},
});
dispatch(hideLoading());
if (response.data.success) {
setUnavailableDates(response.data.data);
console.log(response.data.data);
console.log(setUnavailableDates);
}
} catch (error) {
dispatch(hideLoading());
}
};
useEffect(() => {
getDateData();
}, []);
const disabledDate = (current) => {
// How to pass the dates fecthed from DB here? Direct with setUnavailableDates?
return current && current < moment().endOf("day");
};
The output in network is this:
{
"message": "All Dates Fetched Successfully",
"success": true
}
The output in console is: undefined