"hasMany is not defined" 错误。 Emberjs 中的自反关系

"hasMany is not defined" error. Reflexive relationship in Emberjs

当我点击一个 link 到一个检索具有自反关系的模型的路径时,我得到了这个错误。

Uncaught ReferenceError: hasMany is not defined

这是我在 Ember

中的模型
// app/models/section.js
import Model, { attr } from '@ember-data/model';

export default class SectionModel extends Model {
  @attr('string') title;
  @attr('string') body;
  @attr('number') order;
  @attr('string') slug;
  @hasMany('section', { inverse: 'superior' }) subsections;
  @belongsTo('section', { inverse: 'subsections' }) superior;
}

这是我的路线

import Route from '@ember/routing/route';

export default class DocsRoute extends Route {
    model() {
        return this.store.findAll('section');
    }
}

这是我的 rails 后端模型

# app/models/section.rb
# frozen_string_literal: true

class Section < ApplicationRecord
  validates_presence_of :title
  extend FriendlyId
  friendly_id :title, use: :slugged

  validates :order, numericality: { only_integer: true }
  default_scope -> { order(:order) }

  has_many :subsections, class_name: "Section",
                          foreign_key: "superior_id"
  belongs_to :superior, class_name: "Section", optional: true

  scope :root, -> { where(superior: nil) }
end

这是我的序列化程序

# app/serializers/section_serializer.rb
# frozen_string_literal: true

class SectionSerializer < ActiveModel::Serializer
  attributes :id, :title, :slug, :body, :order
  belongs_to :superior
  has_many :subsections
end

您在此处缺少导入:

import Model, { attr } from '@ember-data/model';

只需像这样添加导入:

import Model, { attr, hasMany } from '@ember-data/model';

问题主要出在我的配置上:

# backend/config/initializers/active_model_serializers.rb
- ActiveModelSerializers.config.adapter = :json
+ ActiveModelSerializers.config.adapter = :json_api
// frontend/app/adapters/application.js
- import ActiveModelAdapter from 'active-model-adapter';
+ import JSONAPIAdapter from '@ember-data/adapter/json-api';
 
- export default ActiveModelAdapter.extend();
+ export default class ApplicationAdapter extends JSONAPIAdapter {
+ }
// app/models/section.js
- import Model, { attr } from '@ember-data/model';
+ import Model, { attr, belongsTo, hasMany } from '@ember-data/model';