Melvin Roest
Melvin Roest

Reputation: 1492

EmberJS: queryRecord - adapter returns a JSON object but the call ultimately returns null

I am dumbfounded and have a hard time debugging this.

This is my adapter, it returns a settings object.

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
  }
});

It returns: settings {id: 1, pencil_thickness: 1, pencil_pressure_sensitivity: 1, eraser_thickness: 20, annotation_options: true, …}

The model is straightforward:

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")
});

This is my route of 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...
  }
});

How come that it returns null and not the settings record?

Upvotes: 0

Views: 244

Answers (1)

Melvin Roest
Melvin Roest

Reputation: 1492

It was an inflector issue. In my model of the world there is no setting, there are only settings.

I found it out by changing queryRecord to query. Since it returns null, the assertion that it query returns an array fails. That particular assertion was quite close to the code that automagically was looking up the model.

By knowing how that worked, I set the right breakpoint at Ember its internal queryRecord method and found the culprit.

Upvotes: 2

Related Questions