我目瞪口呆,很难调试这个。
这是我的适配器,它返回一个设置对象。
import DS from "ember-data";
import * as authServer from "doodledocs-app/config/auth-ms";
import DataAdapterMixin from "ember-simple-auth/mixins/data-adapter-mixin";
export default DS.RESTAdapter.extend(DataAdapterMixin, {
authorizer: "authorizer:devise",
async queryRecord(store, type, data) {
const response = await fetch(`${authServer.HOST}/settings`, {
method: "GET",
headers: { Authorization: data.token }
});
const json = await response.json();
console.log("res settings", json); //this works
return json; //it doesn't matter what I return, queryRecord ultimately returns null
}
});
它会传回:settings {id: 1, pencil_thickness: 1, pencil_pressure_sensitivity: 1, eraser_thickness: 20, annotation_options: true, …}
模型很简单:
import DS from "ember-data";
export default DS.Model.extend({
user: DS.belongsTo("user"),
pencil_thickness: DS.attr("number"),
pencil_pressure_sensitivity: DS.attr("number"),
eraser_thickness: DS.attr("number"),
annotation_options: DS.attr("boolean")
});
这是我的路径settings.js
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
export default Route.extend({
session: service(),
async model() {
const session = this.session.data.authenticated;
const result = await this.store.queryRecord("settings", session);
console.log("result", result); //this returns null...
}
});
为什么它返回null而不是设置记录?
1条答案
按热度按时间ddhy6vgd1#
这是一个偏转器的问题。在我的世界模型中没有
setting
,只有settings
。我通过将
queryRecord
改为query
发现了这个问题。因为它返回null
,所以Assert它query
返回数组失败。这个特定的Assert非常接近于自动查找模型的代码。通过了解它的工作原理,我在Ember的内部
queryRecord
方法上设置了正确的断点,并找到了罪魁祸首。