Nested arrays are not supported

Viewed 22649

The new Firebase database Firestore says

Function DocumentReference.set() called with invalid data. Nested arrays are not supported.

When trying to save the following object:

{
  "desc" : "Blala",
  "geojson" : {
    "features" : [ {
      "geometry" : {
        "coordinates" : [ 8.177433013916017, 48.27753810094064 ],
        "type" : "Point"
      },
      "type" : "Feature"
    } ],
    "type" : "FeatureCollection"
  },
  "location" : {
    "lat" : 48.27753810094064,
    "lng" : 8.177433013916017
  },
  "name" : "Wald und Wiesen",
  "owner" : "8a2QQeTG2zRawZJA3tr1oyOAOSF3",
  "prices" : {
    "game" : {
      "Damwild" : 10,
      "Raubwild" : 300,
      "Rehwild" : 250,
      "Schwarzwild" : 40
    },
    "perDay" : 35
  },
  "rules" : "Keine Regeln!",
  "wild" : {
    "desc" : "kein Wild",
    "tags" : [ "Damwild", "Rehwild", "Schwarzwild", "Raubwild" ]
  }
}

what exactly is the nested array that firestore is complaining about? I can't find it in the documentation.

If it's the GeoJSON object - how would I save it instead?

4 Answers

UPDATE: This was fixed in Firebase JS SDK 4.6.0. Directly nested arrays are still unsupported, but you can now have an array that contains an object that contains an array, etc.

This is a bug in the currently released SDKs.

The backend has the restriction that only directly nested Arrays are unsupported.

In your case you have arrays containing objects containing arrays and the validation logic in the clients is disallowing it when it shouldn't.

There's no public bug tracking this but I'll post back when we have a fix.

You could adapt a serialization function that converts arrays with object types into a map. The keys can be numeric to maintain order.

i.e. { 1: Object, 2: Object2 ... }

On deserialization you can get the Object.values(data); to put it back into an array to be used client-side.

Python:

# Matrix storage in firestore

def matrix_to_fb_data(matrix):
    return [{'0': row} for row in matrix]

def fb_data_to_matrix(fb_data):
    return [row['0'] for row in fb_data]

Firestore doesn't allow 2d arrays, like previous answers have noted, but they allow arrays of maps... of arrays :)

Related