Search code examples
firebasegoogle-cloud-firestorefirebase-security

Firestore Rules - get() behaviour (bug?)


In Firestore rules I created this function:

function rulesTest() {
     let doc = get(/databases/$(database)/documents/someCollection/someDocId);
     return doc == null;
}

According to the documentation of the get() method the return type of it is:

non-null rules.firestore.Resource the document, or null if it does not exist

Testing this function on an empty database (so no someCollection and no someDocId) I am expecting doc to be null and therefore rulesTest() to return true.

Actual return type is false.

The weird part:

Even if I change the the return statement to return doc != null the function would return false.

edit: This is happening on Emulator


Solution

  • This is a Firestore bug: https://github.com/firebase/firebase-tools/issues/2067

    Use this instead:

    function safeget(path) {
       return exists(path) ? get(path) : null;
    }