Firebase 오류

ROCKBELL·2023년 6월 4일

Firebase

목록 보기
2/2

Expected type 'dh', but it was: a custom xn object
FirebaseError: Expected type 'dh', but it was: a custom xn object

Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

  • 변경전
import { query, addDoc } from "firebase/firestore/lite";
  • 변경후
import { query, addDoc } from "firebase/firestore";

timestamp 의 형식의 경우 serverTimestamp() 을 사용

import {
  addDoc,
  collection,
  serverTimestamp,
  Timestamp,
} from "firebase/firestore";

const Chat = () => {  
  const user = useAppSelector((state) => state.user.user);
  const channelId = useAppSelector((state) => state.channel.channelId);
  const [inputText, setInputText] = useState<string>("");

  const docRef = await addDoc(
    collection(
        db,
        "channels",
        String(channelId),
        "messages"
      ),
      {
        timestamp: serverTimestamp(),
        message: inputText,
        user: user,
      }
  );
  ...
};
<span className="messageTimestamp">
  {timestamp?.toDate().toLocaleString()} // 2023. 6. 5. 오전 3:06:04
</span>

0개의 댓글