ember.js EmberJS:queryRecord -适配器返回一个JSON对象,但调用最终返回null

jobtbby3  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(117)

我目瞪口呆,很难调试这个。
这是我的适配器,它返回一个设置对象。

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而不是设置记录?

ddhy6vgd

ddhy6vgd1#

这是一个偏转器的问题。在我的世界模型中没有setting,只有settings
我通过将queryRecord改为query发现了这个问题。因为它返回null,所以Assert它query返回数组失败。这个特定的Assert非常接近于自动查找模型的代码。
通过了解它的工作原理,我在Ember的内部queryRecord方法上设置了正确的断点,并找到了罪魁祸首。

相关问题