0

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?

Here is my data structure:

public class TshirtOrder
{
    public int Id { get; set; }
    public virtual Student Student { get; set; }
    public virtual ICollection<OrderItem> 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; }
}

This is related breeze datacontext.js:

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;
    }
}

When trying to save it at common.saveEntity(order), I got the error

Error: Error while interpolating: {{tshirtOrder.orderItems[0]}}
TypeError: Converting circular structure to JSON

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.

I know this question has been asked, but I checked all answers and couldn't find my solution. The most relevant one is here, and this is the accepted solution:

var cache = [];
JSON.stringify(o, function(key, value) {
    if (typeof value === 'object' && 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

However, it doesn't work for me. As I debug through it, I realize (cache.indexOf(value) !== -1) is never true in my case.

Your help is much appreciated.

Update: This is my saveEntity() method:

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";
        }
    }
}

Where manager is

var manager = new breeze.EntityManager("api/Breeze");
Community
  • 1
  • 1
newman
  • 6,841
  • 21
  • 79
  • 126

1 Answers1

0

What is the code behind common.saveEntity(order)? Are you going thru the EntityManager.saveChanges method?

Jay Traband
  • 17,053
  • 1
  • 23
  • 44
  • Jay, I updated my question with this method included. – newman Nov 08 '13 at 02:19
  • Jay, actually, your answer to my other question (http://stackoverflow.com/questions/19828467/ef-code-first-the-insert-statement-conflicted-with-the-foreign-key-constraint) solved this problem as well. This error message has mislead me. There is no circular reference! – newman Nov 08 '13 at 02:50
  • Sorry, Jay, I spoke a little too early. I get this error back again. Now, I got a complete data set saved without this error. However, when I try to get it back using breeze's expand to include all data, I got this error again. I'll update my question to include this part of problem. – newman Nov 08 '13 at 05:40
  • Actually, I take it back again. It is working - this time there was a problem in my html. The error message is really misleading and confusing. – newman Nov 08 '13 at 05:56
  • If you can create a "simple" example that gives a misleading error message, please post it as a separate SO question and I'll try to take a look. We DO try to give good error messages but... – Jay Traband Nov 08 '13 at 20:14
  • Jay, I just tried...one case that is very easy to reproduce: if you bind to an object instead of a property, you get this generic error. It doesn't tell where the problem is and it could be very time consuming to figure it out. For example, if you have something like {{student.firstName}} in html, which is working fine, now you remove .firstName, so {{student}}, you will get this error. – newman Nov 09 '13 at 15:50
  • This sounds like a binding error. Are you are using Angular? If so, this is likely something that is in their ballpark. – Jay Traband Nov 10 '13 at 17:30