You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// creates new instance via constructor as this is the new entity
returnnewEntity(...params);
That seems to happen when Utils.getParamNames gets to the case 'BindingElement': return p.left.name;, and p.left.name is undefined as the destructuring assignment parameter is unamed.
A possible fix would be to check if p.left is defined but p.left.name is undefined - then it's probably a destructuring assignment and the property names should be found on p.left.properties.
To Reproduce
import{Entity,PrimaryKey}from'@mikro-orm/core';import{v4}from'uuid';
@Entity()classUser{
@PrimaryKey()id? =v4();constructor({ id }: Partial<User>){this.id=id;}}
Calls like em.getRepository(User).create({ id: ' fake-uuid-like-string' }) will fail with Cannot destructure property 'id' of 'undefined' as it is undefined.
Versions
Dependency
Version
node
v12.16.3
typescript
3.9.7
@mikro-orm/core
4.0.0-rc.4
@mikro-orm/postgresql
4.0.0-rc.4
The text was updated successfully, but these errors were encountered:
This use case was not really supported, and the error is not caused by that undefined in ctor params, but rather in your ctor implementation, as you rely on the ctor value, but ORM won't give you any if the parameter is not named as one of the properties.
I was recently thinking about this, and I think we should provide the full data object in the ctor param if there is something either not matching existing properties or not having a name (like your example), which should help in most cases.
Btw the reproduction like this is not really reproducing anything, as EntityFactory will never call the ctor if you provide all the PKs. Not talking about using initializer for the id property, as that will override the id from ctor anyway. To make the test really fail, I did add name: string property and used that in the ctor instead of id.
Describe the bug
When using destructuring assignment on a entity class constructor,
meta.constructorParams
is set as[undefined]
onEntitySchema.setClass
, which is reflected asextractConstructorParams
returning[undefined]
onEntityFactory.createEntity
and the followingreturn new Entity(...params);
failing with a TypeError.mikro-orm/packages/core/src/metadata/EntitySchema.ts
Line 190 in 4fd39fc
mikro-orm/packages/core/src/entity/EntityFactory.ts
Lines 76 to 80 in 4fd39fc
That seems to happen when
Utils.getParamNames
gets to thecase 'BindingElement': return p.left.name;
, andp.left.name
is undefined as the destructuring assignment parameter is unamed.mikro-orm/packages/core/src/utils/Utils.ts
Lines 243 to 244 in 8018c41
A possible fix would be to check if
p.left
is defined butp.left.name
is undefined - then it's probably a destructuring assignment and the property names should be found onp.left.properties
.To Reproduce
Calls like
em.getRepository(User).create({ id: ' fake-uuid-like-string' })
will fail withCannot destructure property 'id' of 'undefined' as it is undefined.
Versions
The text was updated successfully, but these errors were encountered: