javascript - “hasMany 未定义”错误。Emberjs 中的反身关系
问题描述
当我单击指向检索具有自反关系的模型的路由的链接时,我收到此错误。
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';
推荐阅读
- c - hiredis redisCommand 为 Raspberry Pi 4 上的所有内容返回 null
- flutter - Flutter 强制更高的包依赖版本
- python - 为什么我不能运行只有打印功能的 .py 文件?
- keras - “ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=4”
- c# - 在 Unity 的 ScriptableObject 中序列化脚本实例
- c# - 使用 .NET Core 和抽象(基)类进行依赖注入
- reactjs - 我在反应原生时以错误的方式获取 api
- javascript - 使 html 页面的某些部分不可滚动且不可见
- reactjs - firebase facebook弹出窗口太大
- c# - Unity:更新功能期间的触摸控制