0

I'm following an Embercasts course (Ember + Rails). For the screencasts, they used Ember 3.0, but I'm using Octane.

In one video, a custom service is implemented. This is what my version looks like:

import Service, { inject as service } from '@ember/service';

export default class CurrentUserService extends Service {
  @service store;

  load() {
    this.store.queryRecord('user', { me: true })
      .then((user) => {
        this.set('user', user);
      })
      .catch((e) => {
        debugger;
      });
  }
}

In the load function, which is being called from a route, this.store.queryRecord() causes an error:

TypeError: str.replace is not a function
  at Cache.func (index.js:64)
  at Cache.get (index.js:774)
  at decamelize (index.js:167) 
  at Cache.func (index.js:32)
  at Cache.get (index.js:774)
  at Object.dasherize (index.js:190)
  at UserAdapter.pathForType (json-api.js:221)
  at UserAdapter._buildURL (-private.js:293)
  at UserAdapter.buildURL (-private.js:275)
  at UserAdapter.urlForQueryRecord (user.js:13)

The relevant line is

var DECAMELIZE_CACHE = new _utils.Cache(1000, str => str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase());

This is the UserAdapter:

import ApplicationAdapter from './application';

export default class UserAdapter extends ApplicationAdapter {
  urlForQueryRecord(query) {
    if (query.me) {
      delete query.me;

      return `${super.buildURL(...arguments)}/me`;
    }

    return `${super.buildURL(...arguments)}`;
  }
}

What's wrong here?

2
  • how does your adapter look like? I assume str is not a string. Commented Dec 29, 2020 at 1:01
  • Yes, str is undefined. I added the UserAdapter to my question. Commented Dec 29, 2020 at 13:39

1 Answer 1

1

when you do super.buildURL(...arguments) you essentially do super.buildURL(query). And query is an object ({ me: true }) not a string while buildURL expects the modelName as first parameter.

So probably you wanna do something like this instead:

urlForQueryRecord(query, modelName) {
  if (query.me) {
    delete query.me;

    return `${super.buildURL(modelName)}/me`;
  }

  return `${super.buildURL(modelName)}`;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.