Firestore Emulator multiple requests for one operation

56 Views Asked by At

Can somebody please confirm that this behavior is normal for Firestore Emulator?
If not, could this be a bug?
Maybe my firestore.rules aren't set up correctly?


When executing setDoc:

const docRef = doc(this.fs, `users/${userUid}/items`, item.id);
return from(setDoc(docRef, item)).pipe(...

...there are the 3 requests in the emulator

emulator-requests



...details of the requests:

first:

first-create-request



second:

second-create-request



third:

third-create-request



When executing updateDoc:

const docRef = doc(this.fs, `users/${userUid}/items`, update.id);
return from(updateDoc(docRef, update)).pipe(...

...there are the 2 requests in the emulator

enter image description here



...details of the requests:

first:

first-update-request



second:

second-update-request




SECURITY RULES:

function isLoggedInUser(request) {
  return request.auth != null && request.auth.token.email_verified;
}

function isUsersOwnAccount(request, userId) {
  return request.auth.uid == userId;
}

function onlyAllowedKeysChanged(request, resource) {
  return request.resource.data.diff(resource.data).affectedKeys().hasOnly([...])
}

function atLeastOneOfKeysChanged(request, resource) {
  return request.resource.data.diff(resource.data).affectedKeys().has Any([...])
}

match /users/{userId}/items/{itemId} {
    
allow create: if
isLoggedInUser(request)
&& isUsersOwnAccount(request, userId)
&& isValidItem(request.resource.data);
      
allow update: if
isLoggedInUser(request)
&& isUsersOwnAccount(request, userId)
&& atLeastOneOfKeysChanged(request, resource)
&& onlyAllowedKeysChanged(request, resource);


0

There are 0 best solutions below