grahamearley / FirestoreGoogleAppsScript

A Google Apps Script library for accessing Google Cloud Firestore.
http://grahamearley.website/blog/2017/10/18/firestore-in-google-apps-script.html
MIT License
643 stars 107 forks source link

TypeError: Cannot read property 'unwrapObject' of undefined #95

Closed marcolong closed 4 years ago

marcolong commented 4 years ago

It appears with the new version, 30 (I guess starting from 27). It depends on the doc returned, if it contains array of maps or other particular nested structure, it crashes. With a "standard" doc, so without nested object, it works. Works fine until version 26.

Minimal code to reproduce the problem

const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.query('carts').orderBy('lastUpdate', 'desc').limit(10000).execute();

Expected Behavior

documents[0].obj; // This returns raw object.

Actual Results

documents[0].obj; 
// TypeError: Cannot read property 'unwrapObject' of undefined
//  at unwrapValue(Document:62:29)
//   at unwrapArray(Document:79:29)
//   at unwrapValue(Document:64:29)
//   at [unknown function](Document:74:31)
//   at unwrapObject(Document:73:49)
//   at get obj(Document:42:25)
Library Version:

30

LaughDonor commented 4 years ago

As stated in the Breaking Changes:

Query function names have been capitalized (Select, Where, OrderBy, Limit, Offset, Range).

This change was to avoid conflicts with Firestore Typing documentation.

marcolong commented 4 years ago

Sorry, I pasted the old code, but It doesn't also work with the new one:

Minimal code to reproduce the problem

const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.query('carts').OrderBy('lastUpdate', 'desc').Limit(10000).Execute();
LaughDonor commented 4 years ago
const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.query('carts').OrderBy('lastUpdate', 'desc').Limit(10000).Execute();

I tested this in Unit Tests, and it works fine with:

const path = 'Test Collection';
const test = this.db.query(path).OrderBy('boolean value', 'desc').Limit(10000).Execute();

Returns 3 objects as expected, and .obj targets each of them to reveal the full object.

Are you sure your data is correct? You will get 0 results back if you don't have a collection named "carts" or if there are no documents in that collection with the field name "lastUpdate".

marcolong commented 4 years ago

With a standard object it works fine also for me. I have a result that contains an object with a nested array of maps and it crashes. Try to complicate your object in your test, with a field array of maps or something similar.

LaughDonor commented 4 years ago

Try to complicate your object in your test, with a field array of maps or something similar.

@marcolong, Can you provide an example data set? The REST API can't perform OrderBy on a non-comparable field type (Array, Map, Geopoint, Reference).

You can OrderBy a nested field (if it exists). I created the following document:

{
    "map (nested object)": { "foo": 123 }
}

And I was able to retreive it using this.db.query(path).OrderBy('map (nested object).foo', 'desc').Limit(10000).Execute();

LaughDonor commented 4 years ago

@marcolong I see the issue. I misunderstood the problem. I got thrown off because you were using the .query functionality. But it was all about unwrapping a nested object. I'll work on this.