firebasegoogle-cloud-firestorefirebase-security

Firestore security: Incorrect number of arguments supplied to function: diff


I'm trying to use the diff() method in my Firestore rules to ensure the user only edits the allowed fields, however when I try to compile my rules and upload them, I receive the following error in my console:

Incorrect number of arguments supplied to function: diff.

function isEditableFieldForUsersPublic(request) {
  return request.resource.data.diff().changedKeys().hasOnly(['avatarUrl', 'firstName']);
}

How can I fix this error so the diff() function works?


Solution

  • As the error suggests, you need to provide an argument to the diff() method. To get the difference between the new document (request.resource) and the old document (resource), you need to use resource.data as the argument for diff().

    Example from the Firebase docs:

    request.resource.data.diff(resource.data).affectedKeys().hasOnly(["firstName"]);