0

I've built a Javascript web app using Firestore and Firebase. When logging the user out, I am getting console errors. The errors reference the firebase-database.js and firebase-firestore.js scripts, though, so I can't really tell what is happening:

[2020-05-22T12:32:58.436Z] @firebase/database: FIREBASE WARNING: Exception was thrown by user callback. Hr@https://www.gstatic.com/firebasejs/7.6.1/firebase-firestore.js:1:48219 firebase-database.js:1:11297

FirebaseError: Invalid document reference. Document references must have an even number of segments, but user has 1 firebase-firestore.js:1:48219

This is my log out function:

$('.logout').on('click', function(){
    firebase.auth().signOut()
    .catch(function(error){
        console.log(error.code);
        console.log(error.message);
    });
});

Then I have a listener for firebase.auth().onAuthStateChanged which triggers this:

firestoredb.collection('user').doc(uid).update({
    status: false,
    last_changed: firebase.firestore.FieldValue.serverTimestamp()
})
.then(function(){
    uid='';

    $('#screenname').html('');

    window.location='https://www.example.com/your-account.asp?task=logout&afterlogin=%2Fv2';
})
.catch(function(error){
    console.log(error.code);
    console.log(error.message);
});

What might be my strategy for tracking down this error since the console logs are not that helpful? The error does not really affect the performance of the app, since the user is logged out anyway (and redirected via Javascript), however it bothers me that there is an error.

EDIT: I am wondering if the cloud script that is running could be the problem. That might explain why I cannot identify the line number and why the error message is so vague. Here is my cloud script, can this be modified so that a missing UID value would be ignored? This is basically the script provided by Google for combining Firebase and Firestore to maintain session state of the user.

const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp();

const firestore=admin.firestore();

exports.onUserStatusChanged=functions.database.ref('user/{uid}').onUpdate(
    async (change, context) => {
        const eventStatus=change.after.val();
        const userStatusFirestoreRef=firestore.doc(`user/${context.params.uid}`);
        const statusSnapshot=await change.after.ref.once('value');
        const status=statusSnapshot.val();

        if (status.last_changed>eventStatus.last_changed){
            return null;
        }

        eventStatus.last_changed=new Date(eventStatus.last_changed);

        return userStatusFirestoreRef.update(eventStatus);
    }
);
BB Design
  • 695
  • 12
  • 26
  • The Realtime Database and Firestore are two separate databases. Please only tag with the one you're asking about, which in this case is Cloud Firestore. – Frank van Puffelen May 22 '20 at 14:55
  • 1
    Is your `uid` document reference evaluating to what you're expecting it to? – UncaughtTypeError May 22 '20 at 15:03
  • Realtime Database and Firestore are actually both used, in order to maintain session state, and are related to the logout function I'm describing here. The technique for doing this is detailed in Firebase documentation. – BB Design May 22 '20 at 15:25
  • The uid should contain the user's ID, since they were previously logged in, now we are updating them to status FALSE with a new timestamp. I do see the data for the user in Firestore, and the status does change to FALSE in realtime. – BB Design May 22 '20 at 15:28
  • 1
    Have your checked these other questions where they addressed the same message?[1] https://stackoverflow.com/questions/60696369/flutter-platformexception-error-invalid-document-reference-document-refere [2] https://stackoverflow.com/questions/51195351/document-references-must-have-an-even-number-of-segments-error-on-a-collection-r [3] https://stackoverflow.com/questions/59330958/swift-firestore-document-references-must-have-an-even-number-of-segments-but – Antonio Ramirez May 22 '20 at 21:32
  • Those questions all indicated that UID is missing. I could not find anyplace in my script where UID would be missing and tested that with console log. However, I'm wondering if the cloud script I am running could be the reason that I can't identify the line in my script where that would be happening. I will update my question. – BB Design May 26 '20 at 15:15

0 Answers0