nicklandgrebe / active-resource.js

ActiveResource.js - API resource relational mapping in JavaScript
https://active-resource.js.org
MIT License
133 stars 20 forks source link

Uncaught TypeError: Converting circular structure to JSON #2

Closed aehrmann closed 8 years ago

aehrmann commented 8 years ago
  1. Create a resource in-memory: product = Product.build()
  2. Load and assign an associated resource: Venue.first().then (venue) -> product.venue = venue
  3. Do product.save ->

Expected: product saves successfully with product.venue set to loaded venue Actual: Save fails with error: Uncaught TypeError: Converting circular structure to JSON

nicklandgrebe commented 8 years ago

I'll have to make the docs better.

You can't assign two associated resources to each other using a = operator, because of the way we handle associations. For example, you would expect that after assigning the venue to the product, that product would have venueId=...

Not the case, because you did a direct assignment and JavaScript doesn't allow us to override the = operator, so the method to update that foreign key never gets called.

You have to do the following to assign:

product.assignVenue(venue) # local assignment
product.updateVenue(venue) # sends a request to persist the assignment on the server

So call assignVenue, then save