Referencing an object id from another collection

I am building a MERN stack employee management system application. I have created two schemas(one for adding departments and the other for adding employees) I have created and want them to reference each other. These are what I’ve done so far:

  1. Creating a department and inserting it my mongodb database (done)
  2. Displaying departments in table form to the front end (done)
  3. Creating a form to collect employee details to create employee (done)

On this form I have a select html element that is whose dropdown options are populated with the various departments that have already been created. the next step I want to achieve is once the request is made to submit user data to create employee, the object containing the informations for the user e.g. name, email, etc. should include the object id of the particular department that the user chose by referencing that particular department in the departments collection. For now what I get is that when the employee data is submitted to the database, the department mysteriously disappears. I would appreciate if anyone could help me figure this out. I will add code below:

Schema for adding employee:

import mongoose from 'mongoose';
import dotenv from 'dotenv'



dotenv.config()


const MONGOOSE_URL = process.env.URL

mongoose
.connect(MONGOOSE_URL)
.then(() => {
    console.log('Database Connected!')
})
.catch((error)=>{
    console.log(error)
})

const employeeSchema = new mongoose.Schema({
    name: {
        type: String, 
        required: true
    },
    email: {
        type: String, 
        required: true
    },
    password: {
        type: String, 
        required: true
    },
    salary: {
        type: Number
    },
    address: {
        type: String, 
        required: true
    },
    image: {
        type: String
    },
    department_id: { 
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'departments' 
    },
    createdAt: {
        type: Date,
        required: true,
        default: Date.now
    }
})


const employeeModel = new mongoose.model('employees', employeeSchema)


export default employeeModel

Schema for adding department:

import mongoose from 'mongoose';
import dotenv from 'dotenv'


dotenv.config()


const MONGOOSE_URL = process.env.URL

mongoose
.connect(MONGOOSE_URL)
.then(() => {
    console.log('Database Connected!')
})
.catch((error)=>{
    console.log(error)
})

const departmentSchema = new mongoose.Schema({
    name: String
})


const departmentModel = new mongoose.model('departments', departmentSchema)


export default departmentModel

Admin router:

router.post('/add_employee', async (req, res) => {
    const saltRounds = 10;
    const hashedPassword = await bcrypt.hash(req.body.password, saltRounds);

    const user = ({
        "name": req.body.name,
        "email": req.body.email,
        "password": hashedPassword,
        "address": req.body.address,
        "salary": req.body.salary,
        "image": req.body.image,
        "department": req.body.department_id
    })
    
   
        try {
            await employeeModel.create(user);
            return res.json({ Status: true });
        } catch (err) {
            return res.json({ Status: false, Error: 'Query Error' });
        }
   

})

React front end code for populating select element with department options from database:

<div className='col-12'>
                        <label for='department' className='form-label'>
                            Department
                        </label>
                        <select
                            name='department'
                            id='department'
                            className='form-select'
                            onChange={(e) => setEmployee({ ...employee, department_id: e.target.value })}
                        >
                            {department.map(d => {
                                return <option value={d.id}>{d.name}</option>
                            })}
                        </select>
                    </div>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật