[英]Ember destroyRecord (or deleteRecord then save) using ember-cli-mirage failing
What I'm trying to do is integrate ember-cli-mirage into this todo app, https://github.com/ember-cli/ember-cli-todos . 我正在尝试将ember-cli-mirage集成到此待办事项应用程序https://github.com/ember-cli/ember-cli-todos中 。 This app uses Ember 2.0 or greater.
此应用使用Ember 2.0或更高版本。 The setup:
设置:
I verified that the app is working as advertised (except for one minor thing that is not relevant here). 我验证了该应用程序是否按广告进行工作(除了此处不相关的一件小事)。 I can create, update and delete a todo item.
我可以创建,更新和删除待办事项。 The todo app uses ember-data-fixture-adapter/FIXTURES to seed the app with data.
待办事项应用程序使用ember-data-fixture-adapter / FIXTURES为应用程序添加数据。
The steps I did to integrate ember-cli-mirage are: 我整合ember-cli-mirage的步骤是:
I'll post the code for app/mirage/{config.js,factories/post.js,scenarios/default.js} if anybody needs to see it but it's basically just a copy of the instructions in the ember-cli-mirage page (with the "user" model name replaced with "post"). 如果有人需要查看,我将发布app / mirage / {config.js,factories / post.js,scenarios / default.js}的代码,但这基本上只是ember-cli-mirage中指令的副本页(将“用户”模型名称替换为“发布”)。
I restarted the ember server. 我重新启动了ember服务器。 Everything works fine except for the removal of a todo item.
除了删除待办事项以外,其他所有操作都正常。 Record removal is done by pressing the "x" button that will appear when you move your pointer to the right of a todo item.
通过将指针移至待办事项右侧时将出现的“ x”按钮,可以删除记录。 I found out that it fails in the save() part of the record removal.
我发现它在删除记录的save()部分中失败。 The code for the record removal (using the "x" button interface) resides in app/components/todo-item/component.js and it looks like this:
删除记录的代码(使用“ x”按钮界面)位于app / components / todo-item / component.js中,如下所示:
removeTodo() {
var todo = this.get('todo');
todo.deleteRecord();
todo.save();
}
When I try to delete a todo item, the browser console prints "Successful request: DELETE /todos/n" ("n" being the todo id), then it prints a cryptic error message along with the stack trace. 当我尝试删除待办事项时,浏览器控制台将打印“成功请求:DELETE / todos / n”(“ n”为待办事项ID),然后打印一条神秘的错误消息以及堆栈跟踪。
I commented out the "todo.save();" 我注释掉了“ todo.save();” line above.
上面的行。 When I removed a todo item, removal still fails but on the console, there's no error message anymore after the "Successful request: DELETE /todos/n" message.
当我删除待办事项时,删除仍然失败,但在控制台上,“成功请求:DELETE / todos / n”消息之后不再显示错误消息。
So I changed the removeTodo code above in an attempt to make the error messages clearer. 因此,我更改了上面的removeTodo代码,以使错误消息更加清晰。 I changed it to this:
我将其更改为:
todo.save().then(function() {
console.log('Save OK.');
}).catch((err) => {
console.log('Save failed.');
console.log(err.message);
});
I've tried various changes to things here and there but the error message that consistently appears is: 我已经在这里和那里尝试过各种更改,但是始终出现的错误消息是:
Assertion Failed: An adapter cannot assign a new id to a record that already has an id.
断言失败:适配器无法将新ID分配给已经具有ID的记录。 had id: 3 and you tried to update it with undefined.
的ID:3,您尝试用未定义的值对其进行更新。 This likely happened because your server returned data in response to a find or update that had a different id than the one you sent.
之所以可能发生这种情况,是因为您的服务器返回的数据是为了响应与您发送的ID不同的ID的查找或更新。
I saw an error message that had something like "normalizeserializer..." text on it but I forgot to copy the whole message. 我看到一条错误消息,上面有类似“ normalizeserializer ...”的文字,但我忘记了复制整个消息。
I added an adapter: 我添加了一个适配器:
shell> ember g adapter application
// app/adapters/application.js
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
});
But this didn't fix it. 但这并没有解决。
BTW, a todo item creation, which also calls save on a todo item, works. BTW,待办事项创建,也调用保存在待办事项上,可以正常工作。 The code resides in app/components/todos-route/component.js:
该代码位于app / components / todos-route / component.js中:
createTodo() {
const store = this.get('store');
// Get the todo title set by the "New Todo" text field
var title = this.get('newTitle');
if (title && !title.trim()) {
this.set('newTitle', '');
return;
}
// Create the new Todo model
var todo = store.createRecord('todo', {
title: title
});
// Clear the "New Todo" text field
this.set('newTitle', '');
// Save the new model
todo.save();
}
What does your Mirage mock for DELETE
to /todos/:id
look like? 您的Mirage模拟
DELETE
/todos/:id
样子是什么? It looks like you're responding to the DELETE request with a JSON payload that contains an id
, which is causing problems. 看起来您正在使用包含
id
的JSON有效负载响应DELETE请求,这会引起问题。
Instead you could try something that returns an empty response body, like 相反,您可以尝试返回空响应主体的内容,例如
this.del('/todos/:id', (db, request) => {
let id = request.params.id;
db.todos.remove(id);
return new Mirage.Response(204, {}, {});
});
(This code is untested but hopefully you get the idea). (此代码未经测试,但希望您能理解)。
Another small point, there's a destroyRecord
method on models which essentially does deleteRecord
and save
all at once. 还有一点,模型上有一个
destroyRecord
方法,该方法本质上确实执行deleteRecord
并一次save
所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.