-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
TypeError: Cannot convert object to primitive value #2065
Comments
|
this.entity is not null but an object, but it does not inherit from Object (i.e The request object I get from graphQL is being created by Please refer to the conversation in the below pull request, where they are using Object.create(null) to create an object: |
okay.. are you able to provide a PR? |
@shashikiran797 how do you solve this problem? I have the same problem using Nestjs with typescript, graphql and typeorm |
One way to solve this problem is to have constructors to each entity, create objects of entity class and pass them to the tyoeorm methods |
Sure For javascript support is better condition
Note: run on node 10 on my personal pc What do you think @pleerock? Better type check for javascript or just fast bugfix using |
Any news on this? I'm also having issues because of this when using GraphQL with TypeORM. |
@shashikiran797 @PachowStudios Hello, I'd stucked with same problem. I found to instantinate entity with |
You can solve something like this
|
Any news on that? Many other projects are trying to create Objects by However, there are so many Object.create(null) instanceof Object // false Hope we can fix that, or at least put a notice in the documentation. |
Hello, any news? |
I don't know if my issue #4955 is related to this one but if it is, maybe someone can better explain the global issue here and why Typeorm needs prototype based inheritance? |
Won't work if the library you're using like Type-GraphQL creates proto-less objects when passing arguments to mutations for example and you use those same objects to pass to Typeorm to save or update entities. Typeorm probably needs to switch to proto-less objects too since it only uses them as hashmaps (anyway, right?). |
Just ran into this issue using type-graphql, typeorm and nestjs and was scratching my head for a while. This is a really obtuse issue and causes silent bugs. There are workarounds but it really needs to be fixed. |
I solved it for my use case in NestJS with a simple object spread (required for input): @Mutation(() => Item)
public createItem(@Args('input') input: ItemCreateInput): Promise<Item>
return this.itemsService.create({...input}) // spread operator to clone input to new object
} Hope it helps.. |
probably we could just create a sort of Pipe that re-instance the object. |
Just ran into this issue with nestjs and graphql during a find condition and I'm shocked I haven't run into it before. Or have I? |
I've had the same issue with the mongodb driver as well, with the same use case - nestjs + graphql + typeorm while using the same models as the input... can anyone offer a workaround that is more generic then what @jpoppe suggested? |
Due to `instanceof` comparison, object without prototype is mistaken for a privative value, throwing `TypeError: Cannot convert object to primitive value.` when trying to `.toString` said object (inside template string). This mostly effects usage with `graphql` as `graphql-js`, [by design](graphql/graphql-js#504), creates objects by using Object.create(null). This fix allows saving these objects. The fix uses `typeof` instead of `toString` comparison since I see no reason why `new Number/Boolean/Date` should be a supported use case. Closes: #2065
Due to `instanceof` comparison, object without prototype is mistaken for a privative value, throwing `TypeError: Cannot convert object to primitive value.` when trying to `.toString` said object (inside template string). This mostly effects usage with `graphql` as `graphql-js`, [by design](graphql/graphql-js#504), creates objects by using Object.create(null). This fix allows saving these objects. The fix uses `typeof` instead of `toString` comparison since I see no reason why `new Number/Boolean/Date` should be a supported use case. Closes: typeorm#2065
I use NestJS (GraphQL) and TypeORM. To bypass the issue I created a global pipe as shown bellow: import { PipeTransform } from '@nestjs/common/interfaces';
class EnsurePrototypePipe implements PipeTransform {
transform(argument: unknown) {
return ensurePrototype(argument);
}
}
const ensurePrototype = (input: unknown): unknown => {
if (Array.isArray(input)) {
return input.map(it => ensurePrototype(it));
}
if (isObjectLiteral(input) && input.prototype === undefined) {
const output = {};
Object.keys(input).forEach(property => {
output[property] = ensurePrototype(input[property]);
});
return output;
}
return input;
};
export const isObjectLiteral = (v: unknown): v is Record<string | number | symbol, unknown> => {
if (typeof v !== 'object' || v == null) {
return false;
}
const constructor = v.constructor;
return constructor === undefined || constructor === Object;
}; And apply this pipe globally: <...>
nestApp.useGlobalPipes(new EnsurePrototypePipe())
<...> |
Issue type:
[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[x]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
TypeORM version:
[ ]
latest
[ ]
@next
[x]
0.2.0
(or put your version here)Steps to reproduce or a small repository showing the problem:
I am using NestJs with Typeorm
I get the request object form graphql, graphql parameters do not have
__proto__
set.I am directly passing the graphql paramter object to the save method. I am getting the below error when I do that:
When I debugged the issue, it is beacause
EntityPersistExecutor.js checks for the entity to be an instanceof Object, which is false, because
__proto__
is not set for the entityI think changing the above condition
this.entity instanceof Object
totypeof this.entity === 'object'
solves the problemPS: It was working in version 0.1.20.
It is breaking once I upgraded to 0.2.0
The text was updated successfully, but these errors were encountered: