Runtime error: TypeError: Cannot read properties of undefined (reading ‘backdrop’)

I have a vue frontend, where I have to add snippet for one of the additional api call from backend. The vue file was working fine until I added the snippet to accomodate the api request coming from backend. The npm run serve doesnt throw any errors , but when I click on the module on the local frontend associated with vue file I am working on, it gives the run time error. The error is following.

Uncaught runtime errors:
×
ERROR
Cannot read properties of undefined (reading 'backdrop')
TypeError: Cannot read properties of undefined (reading 'backdrop')
    at Modal._initializeBackDrop (webpack-internal:///./node_modules/bootstrap/dist/js/bootstrap.esm.js:2510:39)
    at new Modal (webpack-internal:///./node_modules/bootstrap/dist/js/bootstrap.esm.js:2442:27)
    at Modal.getOrCreateInstance (webpack-internal:///./node_modules/bootstrap/dist/js/bootstrap.esm.js:704:41)
    at HTMLButtonElement.eval (webpack-internal:///./node_modules/bootstrap/dist/js/bootstrap.esm.js:2682:22)
    at HTMLDocument.handler (webpack-internal:///./node_modules/bootstrap/dist/js/bootstrap.esm.js:382:19)

Many articles suggested it might be the issue the DOM not fully loaded and some suggested its the bootstrap issue.

My initial code prior having issues (changes in the template section not shown as it didnt affect the code):

<script>
import StudyReportForm from "../forms/StudyReportForm";
import StudyReportEditForm from "../forms/StudyReportEditForm";

export default {
  name: "ManageStudyReportsModal",
  components: { StudyReportEditForm, StudyReportForm },
  data() {
    return {
      addCollapse: null,
      editCollapse: null,
      isUpdating: false,
      studyreports: [],
      selectedReports: [],
      meta: {
        totalItems: 0,
        totalPages: 0,
        currentPage: 1,
        pageSize: 10,
      },
      report: null,
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    toggleCollapseAddForm() {
      this.isUpdating = false;
      if (this.editCollapse) {
        this.editCollapse.hide();
      }
      if (!this.addCollapse) {
        this.addCollapse = new this.$bootstrap.Collapse(
          document.getElementById("addNewStudyReports")
        );
      }
      this.addCollapse.toggle();
    },
    collapseAddForm(type) {
      this.isUpdating = false;
      if (!this.addCollapse) {
        this.addCollapse = new this.$bootstrap.Collapse(
          document.getElementById("addNewStudyReports")
        );
      }
      if (type === "show") {
        this.addCollapse.show();
        if (this.editCollapse) {
          this.editCollapse.hide();
        }
      } else {
        this.addCollapse.hide();
      }
    },
    collapseEditForm(type) {
      if (!this.editCollapse) {
        this.editCollapse = new this.$bootstrap.Collapse(
          document.getElementById("editStudyReports")
        );
      }
      if (type === "show") {
        this.editCollapse.show();
        if (this.addCollapse) {
          this.addCollapse.hide();
        }
      } else {
        this.editCollapse.hide();
      }
    },
    collapseEditStudyReportsForm() {
      this.collapseEditForm("hide");
      this.report = {};
      this.isUpdating = false;
    },
    reportUpdated() {
      this.collapseEditStudyReportsForm();
      this.getData();
      this.$emit("reportUpdated");
    },
    cancelUpdate() {
      this.collapseEditStudyReportsForm();
    },
    reportAdded() {
      this.addCollapse.hide();
      this.getData();
      this.$emit("reportUpdated");
    },
    async deleteStudyReports(id) {
      await this.$confirm({
        message: `Are you sure?`,
        button: {
          no: "No",
          yes: "Yes",
        },
        callback: (confirm) => {
          if (confirm) {
            this.$repository.studyReports
              .delete(id)
              .then(() => {
                this.getData();
                this.$emit("reportUpdated");
                this.$toast.success("Report deleted");
              })
              .catch(() => {
                this.$toast.error("Deletion Failed");
              });
          }
        },
      });
    },
    editStudyReports(report) {
      if (this.addCollapse) {
        this.collapseAddForm("hide");
      }
      this.isUpdating = true;
      this.report = report;
      this.collapseEditForm("show");
    },
    async getData(page = 1) {
      this.meta.currentPage = page;

      let params = `?page_size=${this.meta.pageSize}&ordering=-uploaded_at`;

      if (this.meta.currentPage > 1) {
        params += `&page=${this.meta.currentPage}`;
      }

      await this.$repository.studyReports.filter(params).then((res) => {
        this.studyreports = res.data.results;
        this.meta.totalItems = res.data.count;
        this.meta.totalPages = Math.ceil(res.data.count / 10);
      });
    },
  },
};
</script>

I added the following change to the script file to (showing the added part only)…Its to accomodate the patch request sent to the backend just to update in_draft: false. Here I modified the mounted method and also added initializeModals() function.

Updated code: (added part only)

import { Collapse } from 'bootstrap';
export default {
........//code //.................
mounted() {
    this.$nextTick(() => {
      this.getData();
      this.initializeModals();
    });
  },
  methods: {
    initializeModals() {
      const addNewStudyReportsElem = document.getElementById("addNewStudyReports");
      if (addNewStudyReportsElem) {
        this.addCollapse = new Collapse(addNewStudyReportsElem, { toggle: false });
      }

      const editStudyReportsElem = document.getElementById("editStudyReports");
      if (editStudyReportsElem) {
        this.editCollapse = new Collapse(editStudyReportsElem, { toggle: false });
      }
    },

//………..added snippet for api below//………….

async handleCheckboxChange(reportId, isChecked) {
      this.isUpdating = true;
      try {
        if (isChecked) {
          // Check if the report is already in the selectedReports list
          if (!this.selectedReports.includes(reportId)) {
            this.selectedReports.push(reportId);
          }

          // Publish the report if checked
          await this.$repository.studyReports.edit({ in_draft: false }, reportId);
          this.$toast.success('Report published successfully.');
        } else {
          // Remove the report from the selectedReports list
          this.selectedReports = this.selectedReports.filter(id => id !== reportId);
        }
      } catch (error) {
        this.$toast.error(`Error: ${error.response?.data?.error || error.message}`);
      } finally {
        this.isUpdating = false;
      }
    }
  },
};
 

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