question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot convert object to primitive value

See original GitHub issue

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:

TypeError: Cannot convert object to primitive value
    at new MustBeEntityError (/Users/shashikiranms/projects/vcbackend/src/error/MustBeEntityError.ts:10:43)
    at EntityPersistExecutor.execute (/Users/shashikiranms/projects/vcbackend/node_modules/typeorm/persistence/EntityPersistExecutor.js:77:35)
    at EntityManager.save (/Users/shashikiranms/projects/vcbackend/src/entity-manager/EntityManager.ts:298:14)
    at UserRepository.Repository.save (/Users/shashikiranms/projects/vcbackend/src/repository/Repository.ts:138:29)

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 entity

if (!this.entity || !(this.entity instanceof Object)) {
     return Promise.reject(new MustBeEntityError_1.MustBeEntityError(this.mode, this.entity));
}

I think changing the above condition this.entity instanceof Object to typeof this.entity === 'object' solves the problem

PS: It was working in version 0.1.20. It is breaking once I upgraded to 0.2.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

15reactions
amille14commented, Oct 30, 2019

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.

9reactions
jpoppecommented, Jan 9, 2020

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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot convert object to primitive value - Stack Overflow
most of the Javascript objects have toString() method inherited from Object.prototype . but some of them that have a null prototype , not...
Read more >
Uncaught TypeError: Cannot convert object to primitive value ...
I'm seeing the following error upgrading from 2.9 to 2.11.0: Uncaught TypeError: Cannot convert object to primitive value toString is being ...
Read more >
Object to primitive conversion - The Modern JavaScript Tutorial
In case of such operations, objects are auto-converted to primitives, and then the operation is carried out over these primitives and results in ......
Read more >
Cannot convert object to primitive value | The Node.js Master ...
Your browser can't play this video. ... Bonus Lecture - TypeError : Cannot convert object to primitive value | The Node.js Master Class....
Read more >
Object to Primitive Conversion in JavaScript - W3docs
String conversion happens when we output an object like alert(obj) and in contexts like that. ToPrimitive. It is possible to enhance string and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found