0

I have an Angular JS application with Breeze and Nancy (self-hosted with Owin). I've figured out how to get data from server with Breeze, but now I'm trying to save changes using Breeze and have problems with it. I've seen MVC examples like:

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
    return _repository.SaveChanges(saveBundle);
}

But obviously I can't do the same with Nancy. My application sends POST request to SaveChanges but then breaks with TypeError: undefined is not a function and Cannot read property 'map' of undefined

At the moment I simply return the same Json I get in Request, as I have no idea what the response should be:

Post["/breeze/SaveChanges"] = parameters =>
{
    string response = "failed";
    try
    {
        response = new StreamReader(this.Request.Body).ReadToEnd();
    }
    catch (Exception ex)
    {
         //TODO handle
    }
    return Response.AsJson(response);
};

I'm not sure if it breaks because it receives incorrect request from server or because I haven't set something up correctly.

Can anyone help with it?

1 Answer 1

1

You can just return the incoming saveBundle -- almost. When the Breeze client receives the save response from the server, it expects it to have two properties: entities and keyMappings. The entities are included in the saveBundle already, but you'll need to add the keyMappings array (which can be empty).

The incoming saveBundle looks like this:

{
  "entities": [
    {
      "OrderId": "4b143db9-6dd4-4c0e-90eb-97520d3694ac",
      "CustomerId": "9ef1c520-318a-4b8a-b99d-cb9f6bdb22cc",
      "OrderDate": "2015-01-30T08:00:00.000Z",
      "entityAspect": {
        "entityTypeName": "Order:#Northwind.Model",
        "defaultResourceName": "Orders",
        "entityState": "Added",
        "originalValuesMap": {
        },
        "autoGeneratedKey": null
       }
    },
    {
     ...more entities...
    }
  ],
  "saveOptions": {
    "tag": "whatever"
  }
}

The outgoing saveResult looks like this:

{
  "entities": [
    {
      "OrderId": "4b143db9-6dd4-4c0e-90eb-97520d3694ac",
      "CustomerId": "9ef1c520-318a-4b8a-b99d-cb9f6bdb22cc",
      "OrderDate": "2015-01-30T08:00:00.000Z",
    },
    {
     ...more entities...
    }
  ],
  "keyMappings": [
  ]
}

Note that the incoming saveBundle has an entityAspect on each entity that describes the entity. The saveResult does not need this, but it's not harmful and will be ignored on the client, as will the saveOptions.

These formats are documented in the DataServiceAdapters section of the Breeze documentation, but it's understandable that you didn't find them.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried to return this response but it doesn't help, the error is still thrown both before any response is received and another one after. I've created another question here: stackoverflow.com/questions/28500013/… and I'll greatly appreciate if you would have a look at that. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.