Firebase VideoChat Error (Invalid document reference. Document references must have an even number of segments, but calls has 1.) [closed]

I am trying to make a web-chat SPA web application using WebRTC, Firebase and React.js . Currently the collection in the database is being created under calls, but inside the document it just says NEW and there is no callId etc to allow me to connect the two users. Have been stuck on this for some time now.. Does anyone see the issue here? The error I am getting is “Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but calls has 1.”

import React, { useState, useRef, useEffect } from 'react';
import { doc, collection, setDoc, updateDoc, getDoc, onSnapshot, addDoc } from 'firebase/firestore';
import { getFirestore } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';

const db = getFirestore();
const auth = getAuth();

const VideoCall = () => {
  const [localStream, setLocalStream] = useState(null);
  const [remoteStream, setRemoteStream] = useState(null);
  const [callId, setCallId] = useState('');
  const [isCaller, setIsCaller] = useState(false);
  const [iceCandidatesQueue, setIceCandidatesQueue] = useState([]);

  const peerConnectionRef = useRef(null);
  const webcamButtonRef = useRef(null);
  const callButtonRef = useRef(null);
  const answerButtonRef = useRef(null);
  const callInputRef = useRef(null);
  const webcamVideoRef = useRef(null);
  const remoteVideoRef = useRef(null);

  const initializePeerConnection = () => {
    if (peerConnectionRef.current) return;

    peerConnectionRef.current = new RTCPeerConnection({
      iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
    });

    peerConnectionRef.current.onicecandidate = (event) => {
      if (event.candidate) {
        const candidatesCollection = isCaller
          ? collection(doc(db, 'calls', callId), 'offerCandidates')
          : collection(doc(db, 'calls', callId), 'answerCandidates');

        addDoc(candidatesCollection, event.candidate.toJSON());
      }
    };

    peerConnectionRef.current.ontrack = (event) => {
      const [remoteStream] = event.streams;
      if (remoteStream) {
        setRemoteStream(remoteStream);
      }
    };
  };

  useEffect(() => {
    if (peerConnectionRef.current && peerConnectionRef.current.remoteDescription) {
      iceCandidatesQueue.forEach(candidate => {
        peerConnectionRef.current.addIceCandidate(candidate);
      });
      setIceCandidatesQueue([]); // Clear the queue after adding
    }
  }, [peerConnectionRef.current?.remoteDescription]);

  const handleWebcamButtonClick = async () => {
    const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
    setLocalStream(stream);

    if (peerConnectionRef.current) {
      stream.getTracks().forEach(track => peerConnectionRef.current.addTrack(track, stream));
    }

    webcamVideoRef.current.srcObject = stream;
  };

  const handleCallButtonClick = async () => {
    initializePeerConnection();

    // Create a new document for the call
    const callDocRef = doc(collection(db, 'calls')); // Create a new document reference in 'calls'
    const offerCandidatesRef = collection(callDocRef, 'offerCandidates');
    const answerCandidatesRef = collection(callDocRef, 'answerCandidates');

    setCallId(callDocRef.id);
    setIsCaller(true);

    // Create offer
    const offerDescription = await peerConnectionRef.current.createOffer();
    await peerConnectionRef.current.setLocalDescription(offerDescription);

    await setDoc(callDocRef, { offer: { sdp: offerDescription.sdp, type: offerDescription.type } });

    onSnapshot(callDocRef, snapshot => {
      const data = snapshot.data();
      if (data?.answer && !peerConnectionRef.current.remoteDescription) {
        const answerDescription = new RTCSessionDescription(data.answer);
        peerConnectionRef.current.setRemoteDescription(answerDescription);
      }
    });

    onSnapshot(answerCandidatesRef, snapshot => {
      snapshot.docChanges().forEach(change => {
        if (change.type === 'added') {
          const candidate = new RTCIceCandidate(change.doc.data());
          if (peerConnectionRef.current.remoteDescription) {
            peerConnectionRef.current.addIceCandidate(candidate);
          } else {
            setIceCandidatesQueue(queue => [...queue, candidate]);
          }
        }
      });
    });
  };

  const handleAnswerButtonClick = async () => {
    initializePeerConnection();

    const callDocRef = doc(db, 'calls', callId);
    const answerCandidatesRef = collection(callDocRef, 'answerCandidates');
    const offerCandidatesRef = collection(callDocRef, 'offerCandidates');

    setIsCaller(false);

    const callData = (await getDoc(callDocRef)).data();
    const offerDescription = callData.offer;

    await peerConnectionRef.current.setRemoteDescription(new RTCSessionDescription(offerDescription));

    const answerDescription = await peerConnectionRef.current.createAnswer();
    await peerConnectionRef.current.setLocalDescription(answerDescription);

    await updateDoc(callDocRef, { answer: { sdp: answerDescription.sdp, type: answerDescription.type } });

    onSnapshot(offerCandidatesRef, snapshot => {
      snapshot.docChanges().forEach(change => {
        if (change.type === 'added') {
          const candidate = new RTCIceCandidate(change.doc.data());
          if (peerConnectionRef.current.remoteDescription) {
            peerConnectionRef.current.addIceCandidate(candidate);
          } else {
            setIceCandidatesQueue(queue => [...queue, candidate]);
          }
        }
      });
    });
  };

  return (
    <div className="videowindow">
      <div className="video-container">
        <video ref={webcamVideoRef} autoPlay muted></video>
        <video ref={remoteVideoRef} autoPlay></video>
      </div>
      <div className="input-container">
        <input 
          ref={callInputRef} 
          type="text" 
          placeholder="Call ID" 
          onChange={(e) => setCallId(e.target.value)} 
        />
      </div>
      <div>
        <button 
          ref={webcamButtonRef} 
          onClick={handleWebcamButtonClick}
        >
          Start Webcam
        </button>
        <button 
          ref={callButtonRef} 
          onClick={handleCallButtonClick}
        >
          Call
        </button>
        <button 
          ref={answerButtonRef} 
          onClick={handleAnswerButtonClick}
        >
          Answer
        </button>
      </div>
    </div>
  );
};

export default VideoCall;

I have tried changing the logic, but cannot see where the issue is.

Recognized by Google Cloud Collective

New contributor

user23962085 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

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