Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

how to store foreach return value in array and send two response in express?

$
0
0

I tried to store the doctor details in array and display appointment and doctor Name Value response. Appointment value is acheived but doctor Name Value comes empty even if it shows in console.

enter image description here

// get specific appointments of user
router.get("/user/:userId", async (req, res) => {

    try {

        console.log("user_id", req.params.userId);
        const user = await User.findById(req.params.userId);
        console.log("user", user);
        const uservalue = user._id;
        console.log("user_value", uservalue);
        const appointment = await Appointment.find({
            user_id: uservalue
        });
        console.log("appointments", appointment);
        const doctorNameValue = [];
        appointment.forEach(async element => {
            const doctor = element.doctor_id;
            const doctorDetails = await Doctor.findById({
                _id: doctor
            });
            doctorNameValue.push(doctorDetails);
            console.log(doctorNameValue);

        });
        res.json({
            appointments: appointment,
            doctorNameValue
        });
    } catch (err) {
        res.status(401).json({
            message: err
        });
    }
});


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>