WebTorrent : TypeError: Cannot read properties of undefined (reading ‘find’)

I’m working on a school project, I’m setting up some kind of streaming website. I’m supposed to be able to stream a movie using WebTorrent asynchronously.
I’m using NodeJS, React and Mongo. I have some kind of ‘home page’ displaying movies thumbnails, when I click on one I get redirected to the streaming page with the torrent id in the url. With that the server can gather data for that movie from database. Here is the code for the streaming page :

import React, { useEffect, useState, useRef } from 'react';
import axios from 'axios';
import { useParams } from 'react-router-dom';

function TorrentStream() {
  const { id } = useParams();
  const videoRef = useRef(null);
  const [movie, setMovie] = useState(null);

  useEffect(() => {
    const fetchMovieDetails = async () => {
      try {
        const response = await axios.get(`https://localhost:3002/api/movies/${id}`);
        setMovie(response.data);
        console.log()
      } catch (error) {
        console.error('Error fetching movie details:', error);
      }
    };

    fetchMovieDetails();
  }, [id]);

  const handleStreamVideo = async () => {
    try {
      const response = await axios.get(`https://localhost:3002/api/movies/stream/${id}`, {
        headers: {
          'Range': 'bytes=0-'
        },
        responseType: 'blob'
      });
      const blobUrl = URL.createObjectURL(response.data);
      if (videoRef.current) {
        videoRef.current.src = blobUrl;
        videoRef.current.load();
        videoRef.current.play();
      }
    } catch (error) {
      console.error('Error fetching video:', error);
    }
  };

I have a small stream button that calls handleStreamVideo on click.This makes a call to this api function :

export const streamMovie = async (req, res) => {
    const { id } = req.params;
    try {
        const movie = await Movie.findById(id);
        if (!movie) {
            console.error('Movie not found');
            return res.status(404).json({ message: 'Movie not found' });
        }

        const magnetURI = movie.magnet_link;
        let existingTorrent = client.get(magnetURI);

        console.log('magnetURI:', magnetURI);
        console.log('existingTorrent:', existingTorrent);

        if (existingTorrent) {
            handleStream(existingTorrent, req, res);
        } else {
            client.add(magnetURI, { path: movie.storage_path }, torrent => {
                torrent.on('ready', () => handleStream(torrent, req, res));
                torrent.on('error', error => console.error('Error adding torrent:', error));
            });
        }
    } catch (err) {
        console.error('Error fetching movie:', err);
        res.status(500).json({ message: 'Error fetching movie', err });
    }
};

and as you can see, this calls one last function :

const handleStream = async (torrent, req, res) => {
    try {
        await torrent.ready;
        const file = torrent.files.find(file => file.name.endsWith('.mp4'));

        if (!file) {
            console.error('No suitable video file found in the torrent');
            return res.status(404).json({ message: 'No suitable video file found in the torrent' });
        }

        const range = req.headers.range;
        if (!range) {
            console.error('Range header is required');
            return res.status(400).send("Requires Range header");
        }

        const positions = range.replace(/bytes=/, "").split("-");
        const start = parseInt(positions[0], 10);
        const total = file.length;
        const end = positions[1] ? parseInt(positions[1], 10) : total - 1;
        const chunksize = (end - start) + 1;

        res.writeHead(206, {
            "Content-Range": `bytes ${start}-${end}/${total}`,
            "Accept-Ranges": "bytes",
            "Content-Length": chunksize,
            "Content-Type": "video/mp4"
        });

        const stream = file.createReadStream({ start, end });
        pump(stream, res, err => {
            if (err) console.error('Stream pipeline failed', err);
        });
    } catch (err) {
        console.error('Error handling stream:', err);
        res.status(500).json({ message: 'Error handling stream', err });
    }
};

Now when I click on the stream button I get this error saying :

Error handling stream: TypeError: Cannot read properties of undefined (reading 'find')
at handleStream (file:///backend/src/api/controllers/movieController.js:72:36)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

talking about this line of code :
const file = torrent.files.find(file => file.name.endsWith('.mp4')); from handleStream function.
I checked in the torrent itself, and there is indeed a .mp4 file, so that’s not the issue. The magnet uri is valid, I was able to download it and check the movie and everything.

I checked the value of existingTorrent in streamMovie with this statement : console.log('existingTorrent:', existingTorrent); and got this result : existingTorrent: Promise { <pending> }.
I understand that there is an issue with loading the torrent or something but I’m really lost here, I’ve been stuck on this for a while now. I just want to have the streaming running when I click on the stream button

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