When are writeFields specified in Firestore requests and what replaces them?

前端 未结 1 628
情话喂你
情话喂你 2021-01-19 06:17

The simulator now displays an error message trying to access request.writeFields.
Before that writeFields in Firestore Security Rules did just not work in

1条回答
  •  一整个雨季
    2021-01-19 06:40

    EDIT Mar 4, 2020: Map.diff() replaces writeFields functionality

    The Map.diff() function gives the difference between two maps: https://firebase.google.com/docs/reference/rules/rules.Map#diff

    To use it in rules:

    // Returns a MapDiff object
    map1.diff(map2)
    

    A MapDiff object has the following methods

    addedKeys() // a set of strings of keys that are in after but not before
    removedKeys() // a set of strings of keys that are in before but not after
    changedKeys() // a set of strings of keys that are in both maps but have different values 
    affectedKeys() // a set of strings that's the union of addedKeys() + removedKeys() + updatedKeys()
    unchangedKeys() // a set of strings of keys that are in both maps and have the same value in both
    

    For example:

    // This rule only allows updates where "a" is the only field affected
    request.resource.data.diff(resource.data).affectedKeys().hasOnly(["a"])
    

    EDIT Oct 4, 2018: writeFields is no longer supported by Firestore and its functionality will eventually be removed.

    writeFields is still valid, as you can see from the linked documentation. What the error message in the simulator is telling you is that it's unable to simulate writeFields, as it only works with requests coming from client SDKs. The simulator itself seems to be incapable of simulating requests exactly as required in order for writeFields to be tested. So, if you write rules that use writeFields, you'll have to test them by using a client SDK to perform the read or write that would trigger the rule.

    0 讨论(0)
提交回复
热议问题