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.

ERROR TypeError: "item.toJSON is not a function

See original GitHub issue

I have a parent object with a collection of children object:

public class EnduserCartModel
{
     ...
     public ICollection<ProductListModel> products {get;set;}
}

In controller :

[HttpPost("")]
... ([FromBody]EnduserModel enduserModel)
{
     ...
}

At the moment of sending the object from Angular to the Server I got this error: ERROR TypeError: "item.toJSON is not a function. This error appears on this line: data["products"].push(item.toJSON());

The code is generated by NSwag Studio: 13.6.0.0 . So here the bug.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
andmattiacommented, Aug 26, 2020

Hi have the same error (V. 13.5.0) but I found the problem in my case is related to a cast.

args.columns = <EntityDto[]>columns; // Fail on toJSON

I see in debug that my object is not create from EntityDto and toJSON in not available I change with this

 let colTyped : EntityDto[] = [];
        if(columns != undefined){
            for(var idx=0; idx < columns.length;idx++){
                var addCol = new EntityDto();
                addCol.technicalName = columns[idx].technicalName;
                colTyped.push(addCol);
            }
        }

And toJSON is valid function

2reactions
Radwa-Moustafacommented, Mar 20, 2022

I came across this issue multiple times and the solution was to make sure that your “item” is an instance of “ProductListModel” in front-end while you’re appending this object to your array before sending it to the back end so when it calls .toJSON() in service-proxies.ts it will find .toJSON() in your object and it will not throw that exception anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getting this error " $.toJSON is not a function "
I'm trying to use jQuery and Ajax and I use this method. but I get the error $.toJSON is not a function in...
Read more >
item.toJSON is not a function #8337
we're having this issue only on one service and on method of the service that is createoredit. please guide me to solve the...
Read more >
command.data.toJSON is not a function : r/Discord_Bots
Been trying to figure this out but I really cant. Without the toJSON function, i get the error DiscordAPIError[50035]: Invalid Form Body.
Read more >
What is the `toJSON()` Function in JavaScript?
In JavaScript, the JSON.stringify() function looks for functions named toJSON in the object being serialized. If an object has a toJSON ...
Read more >
ERROR TypeError: "item.toJSON is not a function
ERROR TypeError : "item.toJSON is not a function.
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