Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullable field #20

Open
cyrilchapon opened this issue Jun 15, 2017 · 6 comments
Open

nullable field #20

cyrilchapon opened this issue Jun 15, 2017 · 6 comments

Comments

@cyrilchapon
Copy link

When updating a document with a mongoose geojson schema, and trying to set it to null, when required: false, I'm getting :

error:  TypeError: Cannot read property 'type' of null
    at SchemaType.Polygon.cast (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose-geojson-schema\index.js:27
5:15)
    at SchemaType.castForQuery (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\schematype.js:952:15)
    at castUpdateVal (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\services\query\castUpdate.js:288
:17)
    at walkUpdatePath (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\services\query\castUpdate.js:20
9:20)
    at castUpdate (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\services\query\castUpdate.js:70:18)
    at Query._castUpdate (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\query.js:2804:10)
    at castDoc (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\query.js:2830:18)
    at Query._findAndModify (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\query.js:2000:17)
    at Query._findOneAndUpdate (C:\Users\Cyril\Code\imagine-network-api\node_modules\mongoose\lib\query.js:1856:8)
    at C:\Users\Cyril\Code\imagine-network-api\node_modules\kareem\index.js:253:8
    at C:\Users\Cyril\Code\imagine-network-api\node_modules\kareem\index.js:18:7
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
@joshkopecek
Copy link
Contributor

I believe the preferred approach would be deleting the property from the containing object rather than setting it as null. If it's null it'll think there's an object to be cast/validated.

@cyrilchapon
Copy link
Author

cyrilchapon commented Jun 15, 2017

I believe the preferred approach would be deleting the property from the containing object rather than setting it as null.

Well that works, but an $unset field doesn't mean the same as a null field.
null is for deliberate absence of value, not set rather means not-really-wanted absence of value.

EDIT: plus

If it's null it'll think there's an object to be cast/validated.

Yeah, that's it, I think one would want to cast null as a geojson polygon (json-type null is ok, plus it works with Object)

@joshkopecek
Copy link
Contributor

If you cast null as an empty GeoJSON object then it's going to want coordinate values, and if those are empty any geospatial index you have on Mongo will fail AFAIK. If the property doesn't exist it will simply be absented from the index.

@cyrilchapon
Copy link
Author

any geospatial index you have on Mongo will fail AFAIK

Actually they are sparse by default, and null is ok with sparse indexes.

@joshkopecek
Copy link
Contributor

Ok I'll need to check and write a test for this. Thanks for bringing it to my attention. Obviously my knowledge of the Mongo API is a bit rusty!

@joshkopecek
Copy link
Contributor

@cyrilchapon I know this is late, but could you give me an example of the schema and data that you were working with? It would be nice to get this working properly with systems requiring sparse indexes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants