EmberJS:queryRecord-适配器返回JSON对象,但调用最终返回null

问题描述 投票:0回答:1

我很傻,很难调试它。

这是我的适配器,它返回一个设置对象。

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

ember.js
1个回答
0
投票

这是一个变形问题。在我的世界模型中,没有setting,只有settings

我通过将queryRecord更改为query找到了。由于它返回null,因此断言它query返回一个数组失败。该特定断言与自动查找模型的代码非常接近。

通过知道如何工作,我在Ember的内部queryRecord方法中设置了正确的断点,并找到了罪魁祸首。

© www.soinside.com 2019 - 2024. All rights reserved.