我有一个基本的crud应用程序用于博客帖子。上面的错误显示在控制台中。我删除了帖子。但是帖子从数据库中删除。我搜索了类似的问题,没有一个答案解决了问题.I使用 JSONAPIAdapter 进行crud操作。
我检查了ember检查器的标志, isDeleted 的标志设置为 true .FYI 记录的数量显示在< em> ember数据是精确的。
deletePost:function(id){
var self = this;
this.get('store').find('post', id)
.then(post => post.destroyRecord())
.then(() => self.transitionToRoute('posts'));
}
我使用nodejs作为后端,mysql作为db.After删除记录后我发送了这样的响应res.status(204).end();
Ember CLI版本2.6.3
答案 0 :(得分:6)
我很确定这个问题是由ember数据的后台重新加载引起的。即使在删除请求之前触发了对api的查找请求,查找请求也会在您触发删除请求后解析。要禁用后台重新加载并强制执行api呼叫,您可以将{reload: true}
传递给findRecord()
来电。这将确保在您触发删除请求之前解析请求已解决。
像这样:
deletePost(id) {
this.get('store').findRecord('post', id, { reload: true }).then((post) => {
return post.destroyRecord();
}).then(() => {
self.transitionToRoute('posts');
});
}
您可以在此处详细了解findRecord
:http://emberjs.com/api/data/classes/DS.Store.html#method_findRecord
将来,我建议不要使用箭头功能速记,因为即使您不想要返回值,它也会一直返回。
答案 1 :(得分:0)
我已经来过几次不同的时间来寻找解决方案。实际上,我找到了一个更好的答案here。他们推荐的方法是使用peekRecord而不是findRecord(假设商品已在商店中),这将是同步的。那里的代码:
// get the comment from the store synchronously, without triggering a fetch
let comment = store.peekRecord("comment", commentId);
// check if there is a comment; is null in the case the store has no comment
with the given id
if (comment) {
comment.destroyRecord(...);
}
这对我有用,{reload: true}
却没有。