exception - 查询确实返回数据时出现奇怪的 Breeze 错误(更新 : Metadata not loading correctly)

标签 exception odata breeze

我正在使用 ODataController 将数据返回给 Breeze 数据服务,这就是数据服务

app.dataservice = (function (breeze) {    
    breeze.config.initializeAdapterInstances({ dataService: "OData" });

    var manager = new breeze.EntityManager('/api/v1/');

    return {
        getRecipePage: getRecipePage
    };

    function getRecipePage(skip, take, searchText) {
        var query = breeze.EntityQuery
                .from("Recipes")
                .orderBy("Name")
                .skip(skip).take(take)
                .inlineCount(true);
        if (searchText) {
            query = query.where("Name", "contains", searchText);
        }

        return manager.executeQuery(query);
    }
})(breeze);

在我的controller中调用getRecipePage函数时,似乎能正常返回数据,但异常奇怪

getDataFunction(skip, take)
        .then(function (largeLoad) {
            $scope.setPagedData(largeLoad, currentPage, pageSize);
        })
        .fail(function (e) {
            debugger;
        });

e 变量有消息 "; ",这是没有意义的。状态为“200 OK”,这很好。

正文包含我的两个实体和 url appers 正确的“/api/v1/Recipes?$orderby=Name&$top=2&$inlinecount=allpages”,如果我导航到它,json 看起来不错:

{
    "$id": "1",
    "$type": "Breeze.WebApi2.QueryResult, Breeze.WebApi2",
    "Results": [
        {
            "$id": "2",
            "$type": "RecipeBook.Web.Angular.Models.RecipeBook.Recipe, RecipeBook.Web.Angular",
            "Name": "1 Boiled Water",
            "Description": "6 Steamy goodness!",
            "Id": 1
        },
        {
            "$id": "3",
            "$type": "RecipeBook.Web.Angular.Models.RecipeBook.Recipe, RecipeBook.Web.Angular",
            "Name": "2 Hot Chocolate",
            "Description": "5 Chocolatey Chocolateness!",
            "Id": 2
        }
    ],
    "InlineCount": 6
}

...这是什么错误?最后,这是堆栈:

Error
    at createError (http://localhost:62576/Scripts/breeze.debug.js:15182:22)
    at http://localhost:62576/Scripts/breeze.debug.js:14971:40
    at http://localhost:62576/Scripts/datajs-1.1.1.js:1671:17
    at XMLHttpRequest.odata.defaultHttpClient.request.xhr.onreadystatechange (http://localhost:62576/Scripts/datajs-1.1.1.js:2587:25)

关于发生了什么的想法???

编辑: 经过大量挖掘,我已经将问题缩小到与读取响应的处理程序相关的范围。在 datajs-1.1.1.js ~line 8100 中有一个 dispatchHandler 函数。我有一个从 OData 调用返回的 requestOrResponse:

它有一个带有上述 json 文本的 body 属性。然而,数据属性是未定义的,但我认为这就是它试图将正文转换成……并且正在寻找一个处理程序来这样做。它的 statusCode 是 200,statusText 是可以的。但是该方法找不到合适的处理程序并抛出:

  throw { message: "no handler for data" };

...这似乎是错误的来源。我只是不知道什么设置不正确,以便我可以补救这种情况。

编辑 2: 这实际上可能是因为元数据(xml)没有被正确解析......,这是它的样子(调试时从datajs handlerRead函数中获取)

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <Schema Namespace="RecipeBook.Web.Angular.Models.Recipe" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
      <EntityType Name="Recipe">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="Name" Type="Edm.String" />
        <Property Name="Description" Type="Edm.String" />
        <Property Name="Steps" Type="Collection(Edm.String)" />
        <NavigationProperty Name="Ingredients" Relationship="RecipeBook.Web.Angular.Models.Recipe.RecipeBook_Web_Angular_Models_Recipe_Recipe_Ingredients_RecipeBook_Web_Angular_Models_Recipe_RecipeIngredient_IngredientsPartner" ToRole="Ingredients" FromRole="IngredientsPartner" />
      </EntityType>
      <EntityType Name="RecipeIngredient">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="IngredientId" Type="Edm.Int32" Nullable="false" />
        <Property Name="Quantity" Type="Edm.Int32" Nullable="false" />
        <Property Name="UnitOfMeasureId" Type="Edm.Int32" Nullable="false" />
        <Property Name="Notes" Type="Edm.String" />
      </EntityType>
      <Association Name="RecipeBook_Web_Angular_Models_Recipe_Recipe_Ingredients_RecipeBook_Web_Angular_Models_Recipe_RecipeIngredient_IngredientsPartner">
        <End Type="RecipeBook.Web.Angular.Models.Recipe.RecipeIngredient" Role="Ingredients" Multiplicity="*" />
        <End Type="RecipeBook.Web.Angular.Models.Recipe.Recipe" Role="IngredientsPartner" Multiplicity="0..1" />
      </Association>
      <EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
        <EntitySet Name="Recipes" EntityType="RecipeBook.Web.Angular.Models.Recipe.Recipe" />
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

编辑 3:

...因此,如果我使用 OData 作为我的数据服务,我需要在 $metadata 中找到的 json 元数据。如果我使用 WebAPI,它会在/Metadata 中查找元数据,这可以是 Edmx 或 json。但是,我的元数据在 $metadata 处作为 Edmx 返回……这是不支持的一件事吗?

http://www.breezejs.com/documentation/breeze-metadata-details

我即将抛弃所有这些(angular、breeze、odata),并按照旧方法进行。

编辑4: 这不是修复,但如果我关闭元数据它“有效”...所以我的问题肯定与元数据未正确加载有关。

var dataService = new breeze.DataService({
    serviceName: "/api/v1/",
    hasServerMetadata: false 
});

最佳答案

看起来您正在使用 Web API。考虑到您可以使用 [BreezeController] 属性装饰您的 Controller ,而不是指定 OData,您可以使用 webApi 来稍微扩展配置。值得一试。

此外,您可能需要配置 Breeze 适配器以使用与 Angular 配对良好的 backingStore。 - http://www.breezejs.com/samples/todo-dataservice (此链接有一些有用的提示,可指导您设置 Breeze 以与 Angular 很好地协同工作)

最后,请记住,第一次使用时设置任何库似乎总是比实际更困难。配置完成后,您几乎再也不会接触配置,Breeze 就可以正常工作,而且非常棒。

编辑

查看此链接,其中简要介绍了 Breeze、Angular、OData 和 Web API - http://sravi-kiran.blogspot.com/2013/11/UsingBreezeJsToConsumeAspNetWebApiODataInAnAngularJsApplication.html

Ward 在这里给出的另一个很好的“我该如何...”回答 -

How to consume OData service with Html/Javascript?

关于exception - 查询确实返回数据时出现奇怪的 Breeze 错误(更新 : Metadata not loading correctly),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550061/

相关文章:

java - 抛出多个自定义异常时如何处理

c++ - 接收器参数和异常

azure - 将 Azure 数据工厂与来自 Microsoft Dynamics Marketing 的 Odata 源结合使用

asp.net-web-api - Web API 2 OData = $格式不工作 : the request is always ignored

javascript - 如何使用 Breeze js 和 Nancy 实现 SaveChanges

python - Shapely : "shapely.geos.TopologicalError: The operation ' GEOSIntersection_r' produced a null geometry"中的多边形相交错误

sockets - 在集群之间运行hadoop distcp -update时发生SocketTimeoutException

odata - 按实体类型限制 Azure TableQuery

entity-framework - Breeze Expand 无法在带有 EF 的 WebAPI 上工作

knockout.js - 调用breeze函数时如何将数据绑定(bind)值作为参数传递