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

Strange behavior when saving arrays with undefined values #644

Closed
arggh opened this issue Sep 12, 2017 · 7 comments
Closed

Strange behavior when saving arrays with undefined values #644

arggh opened this issue Sep 12, 2017 · 7 comments
Labels

Comments

@arggh
Copy link
Contributor

arggh commented Sep 12, 2017

If I try to save my Astronomy document with an array like this var a = [undefined, undefined, 'foobar', undefined], only the two last entries will be saved, resulting in ['foobar', undefined].

Reproduction:

https://github.com/arggh/astronomy-array-issue

How it works:

  1. Run meteor
  2. Observe the type of array we are about to save (displayed on page for convenience)
  3. Click the Save -button
  4. Observe what actually got saved in the database (displayed on page for convenience)
@lukejagodzinski
Copy link
Member

@arggh Yep, indeed there is a bug. Thanks for noticing it. I've already fixed it and I will release a new version that fixes that.

@lukejagodzinski
Copy link
Member

Fixed in Astronomy 2.5.0

@arggh
Copy link
Contributor Author

arggh commented Sep 13, 2017

I confirm the issue is now fixed, thanks! 👍

Btw, is this the "correct" way to define a field, which holds an Array of mixed nulls/undefineds and Strings? (with no validation yet for actually having only nulls/undefineds or Strings obviously)

fields: {
    values: {
      type: Object,
      default: function() {
        return [];
      },
      validators: [
        { type: 'array' }
      ]
    }
  }

@lukejagodzinski
Copy link
Member

Actually, there is no official way of doing it but if it works in your example then it's ok. In fact, an array is just an object so it should work properly.

@arggh
Copy link
Contributor Author

arggh commented Sep 13, 2017

It works, it just looks a bit hacky so I was wondering if I had missed something in the docs.

Maybe it could be a good idea to consider one or many of these solutions:

  • Possible to define multiple types for an Array eg. type: [String, null, Boolean]
  • Possible to define the specified type as optional, meaning, it can also be null or undefined
  • Possible to define Array containing anything: type: []

@lukejagodzinski
Copy link
Member

Possible to define multiple types for an Array eg. type: [String, null, Boolean]

It is actually possible using unions

Possible to define the specified type as optional, meaning, it can also be null or undefined

That would be the best solution. It would be similar to what we have in GraphQL. However, it would require entire redesign of the types system in Astronomy.

Possible to define Array containing anything: type: []

That actually might be the easiest one to do. Will think about implementing it.

@jdalrymple
Copy link

Possible to define multiple types for an Array eg. type: [String, null, Boolean]

It is actually possible using unions

Possible to define the specified type as optional, meaning, it can also be null or undefined

That would be the best solution. It would be similar to what we have in GraphQL. However, it would require entire redesign of the types system in Astronomy.

Possible to define Array containing anything: type: []

That actually might be the easiest one to do. Will think about implementing it.

Unions dont support null types or optional keys afaik

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

No branches or pull requests

3 participants