hammer
hammer

Reputation: 229

BreezeJS SaveChanges - undefined is not a function

I'm very new to BreezeJS and i'm doing something wrong, but not sure what. I am using a 3rd party API for my GET requests and using my own server backend to process the SaveChanges to fire off each request individually to the 3rd party as I can't customize the post/put requests to the exact syntax and post data format I need.

Our Model is dynamic (meaning customers can add new attributes/fields which then flow through from the rest api to our client) so that's why the code looks like it does below, this is the controller:

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    { 
        var context = JsonConvert.DeserializeObject<List<dynamic>>(saveBundle.SelectToken("entities").ToString());

        foreach (var entity in context)
        {
            foreach (JProperty obj in entity)
            {
                if (obj != null)
                {
                    // nothing right now but in future persist somehow
                }
            }
        } 
        // Construct the save result to inform the client that the server has completed the save operation
        var keyMappings = new List<KeyMapping>();
        return new SaveResult()
        {
            Entities = context.Cast<object>().ToList(),
            Errors = null,
            KeyMappings = keyMappings
        }; 
    }

Call stack looks like such:

TypeError: undefined is not a function
    at http://localhost:5749/Scripts/breeze.debug.js:14114:51
    at http://localhost:5749/Scripts/breeze.debug.js:235:26
    at Array.map (native)
    at __map (http://localhost:5749/Scripts/breeze.debug.js:234:15)
    at proto.visitAndMerge (http://localhost:5749/Scripts/breeze.debug.js:14111:16)
    at http://localhost:5749/Scripts/breeze.debug.js:12806:48
    at __using (http://localhost:5749/Scripts/breeze.debug.js:395:16)
    at Object.processSavedEntities (http://localhost:5749/Scripts/breeze.debug.js:12794:13)
    at saveSuccess (http://localhost:5749/Scripts/breeze.debug.js:12776:67)
    at deferred.promise.then.wrappedCallback (http://localhost:5749/Scripts/angular.js:11046:81) undefined 

I traced it to this line in proto.visitAndMerge (line 14114 in breeze.debug.js): if (node.entityAspect.entityState.isDeleted()) {

If you think I'm doing something idiotic I'm all ears too. The third party API can be modified to do a GET accordingly but there is nothing to handle the SaveChanges bundle so as far as I know this is what I need to do.

Any advice would be great.

For reference, I was trying to follow this pattern: Breezejs SaveChanges: is it possible to return a custom SaveResult object, somehow?

Upvotes: 1

Views: 390

Answers (1)

hammer
hammer

Reputation: 229

I figured it out. entityAspect is sent along with the request. So converting it to a dynamic object created an entityAspect property which I then sent back to the client. This entityAspect needed to be removed so that it would not interfere with the JavaScript object.

Here's some code, maybe it helps someone someday:

var entities = JsonConvert.DeserializeObject<List<dynamic>>(saveBundle.SelectToken("entities").ToString());

        foreach (var entity in entities)
        { 
            JObject objEntityAspect = entity["entityAspect"];
            JToken objEntityState = objEntityAspect["entityState"];

            if (objEntityState.Value<string>() == "Modified")
            {
                // make a post with the instance id
            }

            entity.Remove("entityAspect");
        }

entity.Remove("entityAspect") was the key component needed.

Upvotes: 2

Related Questions