来自 Mirage 的 Ember 数据无法显示在 index.hbs 上

Ember data from Mirage fails to display on index.hbs

本文关键字:显示 index hbs Mirage Ember 数据 来自      更新时间:2023-09-26

余烬新手在这里。我一直在关注 Ember 网站上的教程 这里.

我一直在研究这个例子,一切正常......直到我尝试实施幻影。数据永远不会显示在 index.hbs 页面上。

这是我的模型钩子:

import Ember from 'ember';
export default Ember.Route.extend({
  model() {
    return this.store.findAll('rental');
  },
});

我的模式:出租.js

import DS from 'ember-data';
export default DS.Model.extend({
  title: DS.attr('string'),
  owner: DS.attr('string'),
  city: DS.attr('string'),
  type: DS.attr('string'),
  image: DS.attr('string'),
  bedrooms: DS.attr('number')
});

我的索引.hbs:

<h1> Welcome to Super Rentals </h1>
We hope you find exactly what you're looking for in a place to stay.
{{#each model as |rentalUnit|}}
  {{rental-listing rental=rentalUnit}}
{{/each}}

{{#link-to "about"}}About{{/link-to}}
{{#link-to "contact"}}Click here to contact us.{{/link-to}}

最后是我的应用程序/幻影/配置.js:

export default function() {
  this.get('/rentals', function() {
    return {
      data: [{
        type: 'rentals',
        id: 1,
        attributes: {
          title: 'Grand Old Mansion',
          owner: 'Veruca Salt',
          city: 'San Francisco',
          type: 'Estate',
          bedrooms: 15,
          image: 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg'
        }
      }, {
        type: 'rentals',
        id: 2,
        attributes: {
          title: 'Urban Living',
          owner: 'Mike Teavee',
          city: 'Seattle',
          type: 'Condo',
          bedrooms: 1,
          image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg'
        }
      }, {
        type: 'rentals',
        id: 3,
        attributes: {
          title: 'Downtown Charm',
          owner: 'Violet Beauregarde',
          city: 'Portland',
          type: 'Apartment',
          bedrooms: 3,
          image: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg'
        }
      }]
    };
  });
}

我在 Chrome 开发者控制台中收到两条消息:

Mirage:您的 Ember 应用程序尝试获取"http://localhost:4200/assets/vendor.js",但没有定义路由来处理此请求。在 mirage/config.js 文件中定义与此路径匹配的路由。您是否忘记添加命名空间?

和这个警告:

警告:在有效载荷中遇到"数据",但没有找到模型名称"datum"的模型(使用super-rentals@serializer:-rest:.modelNameFromPayloadKey("data"(解析模型名称(

但是,当我看到以下信息时,该信息似乎已成功检索:

成功请求:获取/租赁 对象 {data: Array[3]}

这反映了正确的数据。它只是在它和 index.hbs 之间打破了某个地方,我是新手来弄清楚的。我敢肯定这只是我的一个小误会。任何帮助将不胜感激!

您安装了错误版本的 Ember Data。此外,请确保在安装依赖项时重新启动服务器。

您收到的错误消息意味着您的应用程序正在使用 RESTAdapter(Ember Data 1.x 的默认适配器(。这就是为什么它查看顶级data键并尝试单数化并找到相关模型的原因。

您可以按照最新 Ember CLI 发行版上的说明进行更新。或者,您可以npm install -g ember-cli@beta从头开始。