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

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

问题描述

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

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

这是我的模型:

# 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')

我正在跑步:

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: ------------------------------- 

" rel="nofollow">http://discuss.emberjs.com/t/ember-data-dont-want-to-load-belongsto-relationship/5703/11]

" rel="nofollow">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.

首先对于那些带着 Ember + TypeKey 问题来到这里的人.

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

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

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}}

typeKey 错误是什么意思?

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

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. .

然后我们查看 Chrome Inspector > Network 选项卡,看到所有对部分的请求都很好,我可以看到响应中的所有数据.

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.

但是,在 Ember Inspector > Data 中查看时,列出了这些部分,但对于除 ID 之外的所有详细信息,它们在所有字段中都显示为未定义.

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.

然后,我们查看了从我的本地应用中生成的 json 以及在生产中生成的内容.

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}}

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

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

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}}

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

好吧,疯狂的是我将 Rails 序列化程序文件拼错为:/app/serializers/sections_serializer.rb 当文件名应该是单数时.

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.

简而言之,开发中的 rails 在运行时寻找文件并忽略我的错误,而生产应用程序更严格并且没有以相同的方式找到文件.

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.

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

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

为了获得正确的 JSON,我只需要在 Rails 序列化器中使用 embed: id,我意识到 Id 用于我的其他模型,但不适用于这个模型.像这样修复它.

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 

错误已修复.

我必须修复的最奇怪的错误之一.

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

另一个提示:我们查看并确保我们为相关的 gem 和 bower 组件设置了明确的版本,但这并没有成为问题,但如果生产产生的错误与开发中的错误不同,这是一个明显的起点.

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天全站免登陆