首页 文章

灰烬 - 无法读取未定义的属性'replace'

提问于
浏览
1

我的申请

我试图使用Laravel作为RESTfull API服务器和Ember作为我的fontend框架构建一个相当简单的应用程序

我的后端服务器位于 http://api.example.com/1.0/

我的前端生活在 http://www.example.com/

我刚开始这个项目,所以我只有几个小时的开发时间,所以可能会有一些配置问题,我在这里失踪,但无论如何 .

我试图从我的服务器获取产品列表,并使用ember-data在我的ember应用程序中显示它们

我正在运行ember 2.0.2和ember-data 2.0.0

我在chrome中遇到以下错误 .

错误

处理路径时出错:product无法读取未定义的属性'replace'TypeError:无法在Object.func(http:// localhost:4200 / assets / vendor.js:45832:15)处读取未定义的属性'replace' .cache.get(http:// localhost:4200 / assets / vendor.js:23421:36)在decamelize(http:// localhost:4200 / assets / vendor.js:45874:29)at Object.func(http :// localhost:4200 / assets / vendor.js:45789:12)在Object.dasherize的Object.Cache.get(http:// localhost:4200 / assets / vendor.js:23421:36)处(http:/ /lhosthost:4200/assets/vendor.js:45878:35)在ember $ data $ lib $ system $ normalize $ model $ name $$ normalizeModelName(http:// localhost:4200 / assets / vendor.js:66295:27 )在ember $ data $ lib $ serializers $ json $ serializer $$ default.extend.modelNameFromPayloadKey(http:// localhost:4200 / assets / vendor.js:75184:67)at ember $ data $ lib $ serializers $ json $ Array.map(native)上的serializer $$ default.extend._normalizeResourceHelper(http:// localhost:4200 / assets / vendor.js:75064:30)

档案

在ember中,我生成了一个产品资源,给出了以下文件 .

// app/routes/product.js

import Ember from 'ember';
export default Ember.Route.extend({
  model() {
    return this.store.findAll('product');
  }
});
// app/model/product.js

import DS from 'ember-data';
export default DS.Model.extend({
  name: DS.attr(),
  price: DS.attr()
});

JSON响应

Json从我的api返回 http://api.example.com/1.0/products

{
  "data": [
    {
      "id": "1",
      "name": "dolores",
      "price": "59015",
      "created_at": "2015-09-06 16:18:13",
      "updated_at": "2015-09-06 16:18:13"
    },
    {
      "id": "2",
      "name": "debitis",
      "price": "57449",
      "created_at": "2015-04-07 14:45:16",
      "updated_at": "2015-04-07 14:45:16"
    },
    ...
  ]
}

2 回答

  • 0

    这是适配器/序列化器错误,但它不具有描述性 . JSONAPIAdapter(默认适配器)的Payload错误

    您应该将有效负载修改为:

    {
      "data": [
      {
        "id": "1",
        "type": "products",
        "attributes": {
          "name": "dolores",
          "price": "59015",
          "created-at": "2015-09-06 16:18:13",
          "updated-at": "2015-09-06 16:18:13"
        }
      }, {
        "id": "2",
        "type": "products",
        "attributes": {
          "name": "debitis",
          "price": "57449",
          "created-at": "2015-04-07 14:45:16",
          "updated-at": "2015-04-07 14:45:16"
        }
      }]
    }
    

    或者使用具有此类有效负载的RESTAdapter / Serializer:

    {
      "products": [{
        "id": "1",
        "name": "dolores",
        "price": "59015",
        "created_at": "2015-09-06 16:18:13",
        "updated_at": "2015-09-06 16:18:13"
      }, {
        "id": "2",
        "name": "debitis",
        "price": "57449",
        "created_at": "2015-04-07 14:45:16",
        "updated_at": "2015-04-07 14:45:16"
      }]
    }
    

    如果无法更改响应有效负载,则必须自定义Adapter / Serializer对 . 检查SO上的相关问题 .

    链接详情:

  • 6

    同样的问题发生在我身上 .

    Version details:

    DEBUG: -------------------------------
    ember.debug.js:DEBUG: Ember      : 1.11.0
    ember.debug.js:DEBUG: Ember Data : 1.0.0-beta.14.1
    ember.debug.js:DEBUG: jQuery     : 1.11.1
    DEBUG: -------------------------------
    

    Cause of the issue:

    似乎使用内联 if 帮助程序 also 向Handlebars元素添加属性会导致问题(您在条件中使用的属性是 truefalsenull 还是 undefined ) .

    我-component.hbs

    <button class="btn btn-solve-my-problems" {{action 'solveIt}} {{if isNotSolvable 'disabled'}}>
      Solve my problems!
    </button>
    

    my-component.coffee

    isNotSolveble: (->
      if @get('somethingMeaningful') then true else null
    ).property('somethingMeaningful')
    

    The solution:

    我没有使用内联 if 助手,而只是将 isNotSolvable 的值归因于 disabled 属性( my-component.coffee 文件上没有任何更改) .

    我-component.hbs

    <button class="btn btn-solve-my-problems" {{action 'solveIt}} disabled="{{isNotSolvable}}">
      Solve my problems!
    </button>
    

    PS . :从方法返回的 nulldisabled 属性的操作有关 . 它与此线程中报告的问题无关 . 它只反映了我的情景以及我是如何解决这个问题的 . 更多细节here .

相关问题