简体   繁体   English

Ember.js - 使用ember-cli-mirage作为假模型时未找到模型待办事项

[英]Ember.js - Model todo not found when using ember-cli-mirage for a fake model

I am learning ember.js with this tutorial When using ember-cli-mirage to create a fake model for todos like this 我正在学习使用本教程的 ember.js当使用ember-cli-mirage为这样的待办事项创建假模型时

import Mirage, {faker} from 'ember-cli-mirage';

export default Mirage.Factory.extend({
    title(i) { return 'Todo title ${i + 1}'; },
    complete: faker.list.random(true, false)
});

my mirage config looks like following 我的海市蜃楼配置如下所示

export default function() {
    this.get('/todos', function(db, request) {
        return {
            data: db.todos.map(attrs => (
                {type: 'todos', id: attrs.id, attributes: attrs }
            ))
        };
    });
    this.post('/todos', function(db, request) {
        let attrs = JSON.parse(request.requestBody);
        let todo = db.todos.insert(attrs);
        return {
            data: {
                type: 'todos',
                id: todo.id,
                attributs: todo
            }
        };
    });
    this.patch('/todos/:id', function(db, request) {
        let attrs = JSON.parse(request.requestBody);
        let todo = db.todos.update(attrs.data.id, attrs.data.attributes);
        return {
            data: {
                type: "todos",
                id: todo.id,
                attributes: todo
            }
        };
    });
    this.del('/todos/:id');
}

My confusion is mostly with models. 我的困惑主要是模特。 The name of model is 'todos' and ember somehow changes it to 'todo' when dealing with single record. 模型的名称是'todos',并且在处理单个记录时,ember以某种方式将其更改为“todo”。

From my todos route, I return the model as follows 从我的待办事项路线,我返回模型如下

routes/todos.js 路线/ todos.js

  model() {
        return this.store.findAll('todos');
    }

And then I don't understand the above code - tutorial said it should be this.store.findAll(**'todo'**); 然后我不明白上面的代码 - 教程说它应该是this.store.findAll(**'todo'**); But that does not return any data to ember console. 但这并没有将任何数据返回到ember控制台。 I changed it to 'todos' and I see some output. 我把它改成'todos' ,我看到了一些输出。 (output at the end) (最后输出)

In routes/todos/index.js -- We return modelFor('todos') 在routes / todos / index.js中 - 我们返回modelFor('todos')

  model(){
    return this.modelFor('todos');
  }

And in the template -- todos/index.hbs 并在模板中 - todos / index.hbs

<ul id="todo-list">
  {{#each model as |todo| }}
      {{todo-item todo=todo}}
  {{/each}}
</ul>

I understand that index gets this model from todos.hbs's {{outlet}} todos.hbs looks like below 据我所知,索引从todos.hbs获取此模型{{outlet}} todos.hbs如下所示

<input type="text" id="new-todo" placeholder="What needs to be done?" />

{{#todo-list todos=model}}
  {{outlet}}
{{/todo-list}}

When I run this application, I get the following error. 当我运行此应用程序时,我收到以下错误。

灰烬检查员输出

In the output, I get the data from the get request on / --> this is todos route But I am not getting the access to todos in the todos/index route. 在输出中,我从/ - >获取get请求中的数据这是todos路由但是我没有在todos / index路由中访问todos。

Thanks for the help. 谢谢您的帮助。 All code snippets are from the tutorial. 所有代码片段均来自本教程。

Check the following (things I noticed): 检查以下(我注意到的事情):

  1. Is your model file named "todo.js"? 您的模型文件名为“todo.js”吗? It should be singular... 它应该是单一的......
  2. Your route should be singular findAll('todo') ! 你的路线应该是奇异的findAll('todo')
  3. Your post route in mirage config has a typo: "attributs: todo" should be attributes. 你在海市蜃楼配置中的帖子有一个拼写错误:“attributs:todo”应该是属性。
  4. You are returning JSONAPI formatted data. 您正在返回JSONAPI格式的数据。 Are you using the JSONAPIAdapter and not the RestAdapter for your application adapter? 您是否在为应用程序适配器使用JSONAPIAdapter而不是RestAdapter?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM