Skip to content

getting the "FirebaseError: Invalid document reference" error even though your Firestore database is empty. #8413

@toreal

Description

@toreal

Operating System

web

Browser Version

Chrome / 129.0.6632.0 canary (arm64)

Firebase SDK Version

10.12.5

Firebase SDK Product:

Auth, Firestore

Describe your project's tooling

React app with yarn

Describe the problem

Here is my partial code.

`
function Basic(props) {
const [rememberMe, setRememberMe] = useState(false);
const handleSetRememberMe = () => setRememberMe(!rememberMe);

const navigate = useNavigate();
const location = useLocation();

const [currentProviders,setcurrentProviders]= React.useState(null);

React.useEffect( () => {

myauth.getAuthref().onAuthStateChanged(async (user) => {
  if (user) {
    const db =getFirestore(app)
    var ref = collection(db,"users");

    console.log(ref)

  const q=  query(ref, where("uid", "==", user.uid) )
  const snapshot = await getDocs(q);
    
        if (snapshot.length > 0) {
          console.log(snapshot.length);
          navigate("/");
       
          //  this.setState({ providerData: user.providerData });
        } else {
          const newUserData = {
            uid: user.uid,
            email: user.email
          };
        const uidref=doc(db,"users",user.uid)
        await addDoc(uidref,newUserData);

        if (location.pathname !== '/admin')
        navigate('/admin');
          console.log(snapshot.length);
        }
      
  }
});

});

/**
* Handles the sign up button press.
*/
function handleSignUp() {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Create user with email and pass.
createUserWithEmailAndPassword(myauth.getAuthref(),email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
});

}

}

`

I am trying the signup function to create a new user and delete it in firebase console. Then the error happened.

Uncaught (in promise) FirebaseError: Invalid document reference. Document references must have an even number of segments, but users/uxtl9XTbj7Scbh7CzkJaimOGsdb2/AEBk5LGftHbjrfcQKZy0 has 3.
await
(anonymous) @ auth_impl.ts:741
Promise.then
registerStateListener @ auth_impl.ts:737
onAuthStateChanged @ auth_impl.ts:544
(anonymous) @ index.js:68
commitHookEffectListMount @ react-dom.development.js:23189
commitPassiveMountOnFiber @ react-dom.development.js:24965
commitPassiveMountEffects_complete @ react-dom.development.js:24930
commitPassiveMountEffects_begin @ react-dom.development.js:24917
commitPassiveMountEffects @ react-dom.development.js:24905
flushPassiveEffectsImpl @ react-dom.development.js:27078
flushPassiveEffects @ react-dom.development.js:27023
(anonymous) @ react-dom.development.js:26808
workLoop @ scheduler.development.js:266
flushWork @ scheduler.development.js:239
performWorkUntilDeadline @ scheduler.development.js:533
Show 14 more frames
Show lessUnderstand this error

Steps and code to reproduce issue

  1. createUserWithEmailAndPassword(myauth.getAuthref(),email, password)
  2. delete the user in firebase console(authentication)
  3. error onAuthStateChanged

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions