Search code examples
javascriptfirebasegoogle-cloud-firestorenext.js13zustand

I'm having error as regards deleting document from firebase. The error is coming from the fileId


  async function deleteFile() {
    if (!user || !fileId) return;

    // the pat to the file we want to delete. this indicates the location of the file we want to delete
    const fileRef = sRef(storage, `users/${user.id}/files/${fileId}`);
    const docRef = doc(db, "users", user.id, "files", fileId);
    // const docRef = db.doc("users/user.id/files/fileId");

    try {
      deleteObject(fileRef).then(async () => {
        deleteDoc(docRef).then(() => {
          console.log("Delete!");
        });
      });
    } catch (error) {
      console.log(error);
    }
    setIsDeleteModalOpen(false);
  }

I have the fileId from an zustand appstore which I am passing through the deleteDoc from firestore but it throws an error.

This is actually a dropboxclone from sonny sangha on youtube ,I have debbugged and searched articles but to no avail. [[enter image description here](https://i.sstatic.net/Rf1je.png)](https://i.sstatic.net/kpcao.png)


Solution

  • It looks like there are one two many state.isDeleteModalOpen lines in your useAppStore call which means that fileId is not set to the right value. Remove this extra state.isDeleteModalOpen line.


    Looking at your linting error we can discern that one of the path arguments is not a string.

    No overload matches this call.
      Overload 1 of 3, '(firestore: Firestore, path: string, ...pathSegments: string[]): DocumentReference<DocumentData, DocumentData>', gave the following error:
      Argument of type: '(open: boolean) => void' is not assignable to parameter of type 'string'.
    
    const docRef = doc(db, "users", user.id, "files", fileId);
    // either user.id or fileId is not a string
    

    Taking a closer look at your useAppStore destructuring call:

    const [isDeleteModalOpen, setIsDeleleModalOpen, fileId, setFileId] =
      useAppStore((state) => [
        state.isDeleteModalOpen,
        state.setIsDeleleModalOpen,
        state.setIsDeleleModalOpen,
        state.fileId,
        state.setFileId
      ]);
    
    // fileId is a '(open: boolean) => void' (and was set to state.setIsDeleleModalOpen)
    

    As can be seen above, you have 5 array elements being destructured as an array of 4 elements. Removing the extra line fixes this problem.

    const [isDeleteModalOpen, setIsDeleleModalOpen, fileId, setFileId] =
      useAppStore((state) => [
        state.isDeleteModalOpen,
        state.setIsDeleleModalOpen,
        state.fileId,
        state.setFileId
      ]);
    
    // fileId is a 'string' (and was set to state.fileId)