title: Breeze/Angular: TypeError: Converting circular structure to JSON
body: <p>First off, I'm new to breeze/angular...after reading some blogs and watching some pluralsight videos, I (like everybody else I guess) was so excited, but after a few weeks of trying, I started hating it because I'm running from one problem to the other. Is this technology still too early to learn and use? </p>
  
  <p>Here is my data structure:</p>
  
  <pre><code>public class TshirtOrder
  {
      public int Id { get; set; }
      public virtual Student Student { get; set; }
      public virtual ICollection&lt;OrderItem&gt; OrderItems { get; set; }
  }
  
  public class OrderItem
  {
      public int Id { get; set; }
      [ForeignKey("Type")]
      public int TshirtTypeId { get; set; }
      public virtual TshirtType Type { get; set; }
      [ForeignKey("Size")]
      public int TshirtSizeId { get; set; }
      public virtual TshirtSize Size { get; set; }
      public double UnitPrice { get; set; }
      public int Quantity { get; set; }
  
      [ForeignKey("TshirtOrder")]
      public int TshirtOrderId { get; set; }
      public TshirtOrder TshirtOrder { get; set; }
  }
  </code></pre>
  
  <p>This is related breeze datacontext.js:</p>
  
  <pre><code>function _createTshirtOrder() {
      var order = manager.createEntity("TshirtOrder");
      order.tshirtItems = [];
      order.tshirtItems.push(createOrderItem(lookups.tshirtTypes[0], lookups.tshirtSizes[0], 10));
      common.saveEntity(order);
      return order;
  
      function createOrderItem(type, size, unitPrice) {
          var item = manager.createEntity("OrderItem");
          item.type = type;
          item.size = size;
          item.unitPrice = unitPrice;
          item.quantity = 0;
          return item;
      }
  }
  </code></pre>
  
  <p>When trying to save it at <code>common.saveEntity(order)</code>, I got the error</p>
  
  <pre><code>Error: Error while interpolating: {{tshirtOrder.orderItems[0]}}
  TypeError: Converting circular structure to JSON
  </code></pre>
  
  <p>The error complains about the circular structure, but I don't have any circular structure at all. In fact, the structure is very standard one and I had the almost same structure working a couple weeks earlier in another trial project. I'm not sure if the new libraries I'm using now has introduced some new defects.</p>
  
  <p>I know this question has been asked, but I checked all answers and couldn't find my solution. The most relevant one is <a href="https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json">here</a>, and this is the accepted solution:</p>
  
  <pre><code>var cache = [];
  JSON.stringify(o, function(key, value) {
      if (typeof value === 'object' &amp;&amp; value !== null) {
          if (cache.indexOf(value) !== -1) {
              // Circular reference found, discard key
              return;
          }
          // Store value in our collection
          cache.push(value);
      }
      return value;
  });
  cache = null; // Enable garbage collection
  </code></pre>
  
  <p>However, it doesn't work for me. As I debug through it, I realize <code>(cache.indexOf(value) !== -1)</code> is never true in my case.</p>
  
  <p>Your help is much appreciated.</p>
  
  <p>Update:
  This is my saveEntity() method:</p>
  
  <pre><code>function _saveEntity(masterEntity) {
      // if nothing to save, return a resolved promise
      if (!manager.hasChanges()) { return $q(); }
  
      var description = describeSaveOperation(masterEntity);
  
      return manager.saveChanges().then(saveSucceeded).fail(saveFailed);
  
      function describeSaveOperation(entity) {
          var statename = entity.entityAspect.entityState.name.toLowerCase();
          var typeName = entity.entityType.shortName;
          var title = entity.title;
          title = title ? (" '" + title + "'") : "";
          return statename + " " + typeName + title;
      }
  
      function saveSucceeded() {
          logger.logInfo("saved " + description);
      }
  
      function saveFailed(error) {
          var msg = "Error saving " + description + ": " + getErrorMessage(error);
  
          masterEntity.errorMessage = msg;
          logger.logError(msg);
          // Let user see invalid value briefly before reverting
          $timeout(function () { manager.rejectChanges(); }, 1000);
          throw error; // so caller can see failure
      }
  
      function getErrorMessage(error) {
          var reason = error.message;
          if (reason.match(/validation error/i)) {
              reason = getValidationErrorMessage(error);
          }
          return reason;
      }
  
      function getValidationErrorMessage(error) {
          try { // return the first error message
              var firstItem = error.entitiesWithErrors[0];
              var firstError = firstItem.entityAspect.getValidationErrors()[0];
              return firstError.errorMessage;
          } catch (e) { // ignore problem extracting error message 
              return "validation error";
          }
      }
  }
  </code></pre>
  
  <p>Where manager is     </p>
  
  <pre><code>var manager = new breeze.EntityManager("api/Breeze");
  </code></pre>