Home > database >  Can't write data as nested maps to Firestore in Swift
Can't write data as nested maps to Firestore in Swift

Time:01-18

I need a database structure that consists of nested maps such as the following example:

enter image description here

The upper map structure is: (key: "referrals", value: (key: userId, value: aBooleanValue))

So I used the following code to accomplish the result above:

let userId = ...
let aBooleanValue = ...

database.(...).setData(["referrals": [userId: aBooleanValue]])

But the code above causes an array inside of a map like the figure below instead of a nested map:

enter image description here

Any ideas how I could get rid of the array here?

EDIT: It's my bad. I was modifying the database as an array in another part of the app. So there is no bug here. Sorry.

CodePudding user response:

If you want referrals to be a key in the map then you must first declare the map, which you never do. The above code, btw, gave me a map, not an array. But the map was named referrals and its only key was userId. Regardless, to make referrals a key itself, define the parent map:

Firestore.firestore().document("test/test").setData([
    "containerMap": [
        "referrals": ["user1": true, "user2": true],
        "contacts": ["user5": true, "user7": true]
    ]
])
  •  Tags:  
  • Related