22

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ProblemsOfSumit
  • 19,543
  • 9
  • 50
  • 61
  • 2
    Funny thing is that this doc suggests nested arrays are supported. Maybe it's a bug in either the code or the doc? https://cloud.google.com/firestore/docs/manage-data/add-data#data_types – Doug Stevenson Oct 05 '17 at 21:14
  • 2
    Did you ever resolve this? I'm getting the same error, using CodableFirebase. – jbm Mar 12 '18 at 04:44
  • 1
    @ProblemsOfSumit, can you confirm if you were able to save the geoJSON. I still get 'FIRInvalidArgumentException', reason: 'Nested arrays are not supported' – Emin Israfil iOS Jun 01 '18 at 00:08

5 Answers5

24

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.

Michael Lehenbauer
  • 16,229
  • 1
  • 57
  • 59
Gil Gilbert
  • 7,722
  • 3
  • 24
  • 25
11

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.

Troy Michael
  • 233
  • 2
  • 7
4

Can't comment so here it goes: this is fixed in 4.6.0, see release notes: https://firebase.google.com/support/release-notes/js#4.6.0

Cloud Firestore

FIXED Fixed the validation of nested arrays to allow indirect nesting.

anotherdev
  • 312
  • 2
  • 11
2

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 :)

Ryan Cocuzzo
  • 3,109
  • 7
  • 35
  • 64
0

You can alternative insert a document with nested arrays and objects with stringified format; for me, in JavaScript, it's:

JSON.stringfiy(someNestedObject);
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77