Ember.js错误|无法阅读财产'一些'未定义的

时间:2016-06-05 17:49:52

标签: ruby-on-rails ember.js ember-data ember-router

我使用Ember作为前端,我正在进行基本测试,看看我是否可以在添加组件之前正确渲染数据。我有两个资源'主题'和'评级'我为这些资源添加了路由和模型钩子。当我输入http://localhost:4200/topics时,我能够看到模板上呈现的所有主题。但是,当我键入http://localhost:4200/ratings时,我在控制台上收到错误消息:

ember.debug.js:32096TypeError: Cannot read property 'some' of undefined
    at error (route.js:21)
    at Object.triggerEvent (ember.debug.js:28580)
    at Object.trigger (ember.debug.js:53473)
    at Object.Transition.trigger (ember.debug.js:53287)
    at ember.debug.js:53107
    at tryCatch (ember.debug.js:53806)
    at invokeCallback (ember.debug.js:53821)
    at publish (ember.debug.js:53789)
    at publishRejection (ember.debug.js:53724)
    at ember.debug.js:32054

这很奇怪,因为在我的rails控制台中,我收到了HTTP:200响应。我的路线代码中是否有错误?我确保镜像与主题类似。或者这是一个关联问题? USER和TOPIC都有很多评级。我在下面提供了我的代码片段:

申请路线:

import Ember from 'ember';

export default Ember.Route.extend({
  auth: Ember.inject.service(),
  flashMessages: Ember.inject.service(),

  actions: {
    signOut () {
      this.get('auth').signOut()
      .then(() => this.transitionTo('sign-in'))
      .then(() => {
        this.get('flashMessages').warning('You have been signed out.');
      })
      .catch(() => {
        this.get('flashMessages')
        .danger('There was a problem. Are you sure you\'re signed-in?');
      });
      this.store.unloadAll();
    },

    error (reason) {
      let unauthorized = reason.errors.some((error) =>
        error.status === '401'
      );

      if (unauthorized) {
        this.get('flashMessages')
        .danger('You must be authenticated to access this page.');
        this.transitionTo('/sign-in');
      } else {
        this.get('flashMessages')
        .danger('There was a problem. Please try again.');
      }

      return false;
    },
  },
});

评级模型:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships';

export default Model.extend({
  score: attr('number'),
  user: belongsTo('user'),
  topic: belongsTo('topic')
});

评级路线:

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.get('store').findRecord('rating', params.id);
  },
});

```

评级路线:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.get('store').findAll('rating');
  },
});

路由器:

import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType,
});

Router.map(function () {
  this.route('sign-up');
  this.route('sign-in');
  this.route('change-password');
  this.route('users');
  this.route('topics');
  this.route('topic', { path: '/topics/:id'});
  this.route('ratings');
  this.route('rating', { path: '/ratings/:id'});
  // Custom route in topics controller that will call NYT API or generate random-show
  //topic. This is a GET request essentially
  this.route('random-show');
});

export default Router;

1 个答案:

答案 0 :(得分:0)

解决了!阅读DOCS,并使用了EXPLICIT INVERSNESS:

https://guides.emberjs.com/v2.5.0/models/relationships/

显然,Ember需要帮助理解当你拥有多个或属于同一类型的多个时。