将ember-rails部署到Heroku - TypeError:无法读取未定义的属性“typeKey” [英] Deploying ember-rails to Heroku - TypeError: Cannot read property 'typeKey' of undefined

查看:111
本文介绍了将ember-rails部署到Heroku - TypeError:无法读取未定义的属性“typeKey”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分页面加载然后空白,我收到以下错误。

  TypeError:无法读取属性'typeKey'未定义的
在Ember.Object.extend.modelFor(http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:12298)
在Ember.Object.extend .recordForId(http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:8860)
at s(http://my-app.herokuapp.com/assets/ manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:1790)
at http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:1524
at http:/ /my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:18:32392
at http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9 :22231
在Object.l.forEach(http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9:21553)
在Object.u。 forEach(http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9:22198)
在Function.a.reopenClass.eachRelationship(http://my-app.herokuapp .com / assets / manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:18:32368)
at a(http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:1302) manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9n.function.n.apply.r

这是我的模型:

 #app / models / plan.js.coffee 
App.Plan = DS.Model.extend
名称:DS.attr('string')
slug:DS.attr('string')
项目:DS.belongsTo('project')
部分:DS.hasMany 'section',{async:true})

#app / models / section.js.coffee
App.Section = DS.Model.extend
name:DS.attr ('string')
详细信息:DS.attr('string')
sort_order:DS.attr('number')
plan:DS.belongsTo('plan')
summary_of_changes: DS.attr('string')

#app / models / project.js.coffee
App.Project = DS.Model.extend
name:DS.attr('字符串')

我正在运行:

  DEBUG:------------------------------- 
DEBUG: Ember:1.7.1 + pre.c1ec09ad
DEBUG:Ember数据:1.0.0-beta.9
DEBUG:Handlebars:1.3.0
DEBUG:jQuery:1.10.2
DEBUG:-------------------------------


rel =nofollow> http://discuss.emberjs.com/t/ember-data-dont-想要加载属性关系/ 5703/11]

解决方案

我有一些帮助,事实证明是一个奇怪的错误。



首先为那些来到Ember + TypeKey问题的人。



查找有关问题的线索



我个人设法将我的错误缩小到我的代码的这一部分阅读堆栈跟踪并看到Object.l.forEach,并意识到该页面上只有一个。

  {{#each section in model.sections}} 
< h2>
{{section.name}}
{{link-to'edit''section.edit'section classNames ='edit'}}
< / h2>
{{markdown section.details}}
{{/ each}}



类型键错误是什么意思?



正如我所解释的那样,link-to helper传递一个对象,上面它应该是一个section对象。 Ember通过调用object.typeKey来询问它的类型。但是在这种情况下,对象缺少,所以我们得到的错误消息基本上是说我试图通过请求它的typeKey但没有定义对象来查找对象的类型。



解决这个问题的方法是询问为什么对象未定义,很可能是因为数据源没有被正确序列化。



我们如何找到解决方案



然后我们查看了Chrome检查器>网络标签看到所有的部分请求都进来了,我可以看到响应中的所有数据。



但是,当查看Ember Inspector> Data时,这些部分被列出,但所有的细节,除了它们在所有领域中未定义的id。



然后我们看了我的应用程序在本地出现的json,什么是来{id:69,name:Whatever,plan:{id:11, name:Sample Project,slug:null,project_id:78}}



Ember实际上不希望在JSON中嵌入记录, id的谢谢你
所以应该看起来像这样



{section:{id:69,name:Whatever,plan_id 11}}



很奇怪的是我拼错了我的rails serializer文件:
/ app / serializers / section s _serializer.rb当文件名应该是单数的时候。



在开发过程中,很短的时间是在运行时寻找该文件,并且在生产应用程序是更严格的是没有以相同的方式找到该文件。



一旦我重命名文件来匹配,我突然能够在我的本地机器上产生typeKey错误



修复很简单。



为了实现正确的JSON,我只需要使用embed:id我实现Id的rails serializer用于我的其他型号,但不是这个。修正它像这样。

  class SectionSerializer< ActiveModel :: Serializer 
属性:id,:name,:info
embed:id#添加此行以获取您的rails serializer设置像Ember喜欢
end

错误已修复。



其中一个最奇怪的错误我不得不修复。



另一个提示:我们查看并确保我们为相关的宝石和凉鞋组件设置了明确的版本,但这并不表示为一个问题,但如果生产产生的错误比开发中的错误是一个明显的起点。


Part of the page loads then blank and I get the following error.

TypeError: Cannot read property 'typeKey' of undefined
    at Ember.Object.extend.modelFor (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:12298)
    at Ember.Object.extend.recordForId (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:8860)
    at s (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:1790)
    at http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:1524
    at http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:18:32392
    at http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9:22231
    at Object.l.forEach (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9:21553)
    at Object.u.forEach (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9:22198)
    at Function.a.reopenClass.eachRelationship (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:18:32368)
    at a (http://my-app.herokuapp.com/assets/manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:19:1302) manifest-docs-cbb67138a1a7e2bbc15fdbae9b24aa06.js:9n.function.n.apply.r

Here are my models:

# app/models/plan.js.coffee
App.Plan = DS.Model.extend
  name: DS.attr('string')
  slug: DS.attr('string')
  project: DS.belongsTo('project')
  sections: DS.hasMany('section',{async: true})

# app/models/section.js.coffee
App.Section = DS.Model.extend
  name: DS.attr('string')
  details: DS.attr('string')
  sort_order: DS.attr('number')
  plan: DS.belongsTo('plan')
  summary_of_changes: DS.attr('string')

# app/models/project.js.coffee
App.Project = DS.Model.extend
  name: DS.attr('string')

I'm running:

DEBUG: ------------------------------- 
DEBUG: Ember      : 1.7.1+pre.c1ec09ad 
DEBUG: Ember Data : 1.0.0-beta.9 
DEBUG: Handlebars : 1.3.0 
DEBUG: jQuery     : 1.10.2 
DEBUG: ------------------------------- 

[Edit: Added project model code] [Edit 2: Found relevant discussion on the ember forums here: http://discuss.emberjs.com/t/ember-data-dont-want-to-load-belongsto-relationship/5703/11]

解决方案

I got some help with this and it turned out to be a strange bug indeed.

Firstly for those of you who come here with Ember + TypeKey issues.

Finding clues as to where the issue is

I personally had managed to narrow my error down to this part of my code by reading down the stack trace and seeing the "Object.l.forEach" and realising there was only one forEach on that page.

            {{#each section in model.sections}}
                <h2>
                    {{section.name}}
                    {{link-to 'edit' 'section.edit' section classNames='edit'}}
                </h2> 
                {{markdown section.details}}
            {{/each}}

What does the typeKey error mean?

As I was explained it, the link-to helper is passed an object, above it should be a section object. Ember asks the object it's type by calling object.typeKey. But in this case the object is missing so then we get the error message which is basically saying "I tried to look up the type of the object by requesting it's typeKey but no object is defined".

The way to address this is to ask the question why is the object undefined, and in all likelihood it could be because the data source is not being serialized correctly. .

How we found the solution

We then looked in Chrome Inspector > Network tab and saw that all the requests for sections were coming in just fine and I could see all the data in the responses.

BUT, when looking in Ember Inspector > Data, the sections were listed but for all the details except the id they showed as undefined in all the fields.

We then looked at the json coming out of my app locally and what was coming out in production.

{"section":{"id":69,"name":"Whatever","plan":{"id":11,"name":"Sample Project","slug":null,"project_id":78}}

Ember actually doesn't want embedded records in the JSON, just id's thankyou. So it should have looked like this

{"section":{"id":69,"name":"Whatever","plan_id":11}}

Well the craziness is that I had misspelled my rails serializer file to be: /app/serializers/sections_serializer.rb when the file name should have been the singular.

In short rails in development was hunting for the file at runtime and overlooking my error while the production app was more strict and wasn't finding the file in the same way.

Once I renamed the file to match, I was suddenly able to produce the typeKey error on my local machine.

The fix was simple.

To achieve the correct JSON I just needed to use embed: id in the rails serializer which I realised Id used for my other models but not for this one. Fixing it like so.

class SectionSerializer < ActiveModel::Serializer
    attributes :id, :name, :info
    embed :id   # add this line to get your rails serializer to set things up like Ember likes
end 

And the error was fixed.

One of the weirdest bugs I've had to fix.

Another tip: We looked and made sure we had set explicit versions for relevant gems and bower components but this didn't turn out to be an issue, but is an obvious starting point if production is producing different errors than in development.

这篇关于将ember-rails部署到Heroku - TypeError:无法读取未定义的属性“typeKey”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆