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

Immutable field on a nested class #660

Open
mazak opened this issue Mar 5, 2018 · 3 comments
Open

Immutable field on a nested class #660

mazak opened this issue Mar 5, 2018 · 3 comments

Comments

@mazak
Copy link

mazak commented Mar 5, 2018

I'm using a nested class with an _id field that I'd like to have be unique and readOnly. This is my setup:

const NestedClass = Class.create({ 
// ... 
  fields: {
    _id: {
      type: String,
      immutable: true,
      default() {
        return new Mongo.Mongo.ObjectID()._str;
      },
    },
  }
// ...
});
const MainClass = Class.create({
// ...
  fields: {
    nested: {
      type: [NestedClass],
      optional: true,
      default() {
        return [];
      },
    },
  },
// ...
});

Only the first element of the nested class added to the object is added with the _id field. Every object that I add more has the _id field absent, or removed if passed along in new NestedClass({ _id: 'something' });.
I've tried playing with setting different combinations of optional, default and immutable and it always boils down to immutable. When that's set to false - it works fine.
Turning it off is fine as a temporary solution, but if this issue can't be fixed I'll have to think of some additional verification to make sure that my ID stays untouched.
Is that a bug? Can that be fixed?

Thank you for this great package!

@lukejagodzinski
Copy link
Member

Please, create reproduction repository so I can test it.

@paul8046
Copy link

paul8046 commented Feb 7, 2019

I can confirm this bug. If a nested field is set to immutable then only the first nested element will contain those values. Subsequently added nested element will not containe the immutable values. My code is as follows:

const Player = Class.create({
	name: 'Player',
	fields: {
		id: {
			type: String,
			immutable: true,
		},
        ...

const Game = Class.create({
	name: 'Game',
	collection: Games,
	fields: {
		player1: {
			type: Player,
			optional: true,
		},
                player2: {
			type: Player,
			optional: true,
		},
        ...

Then in a class Game helper:

this.player1 = new Player({id: id1});
this.player2 = new Player({id: id2});

Now only this.player1.id will be set; this.player2.id will be undefined and not saved.

@lukejagodzinski
Copy link
Member

Can you create reproduction repository?

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

3 participants