0

我正在学习 Embercasts课程(Ember + Rails)。对于截屏视频,他们使用了 Ember 3.0,但我使用的是 Octane。

在一个视频中,实现了自定义服务。这是我的版本的样子:

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

load从路由调用的函数中,this.store.queryRecord()会导致错误:

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)

相关线路是

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

这是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)}`;
  }
}

这里有什么问题?

4

1 回答 1

1

当你这样做时,super.buildURL(...arguments)你基本上是这样做super.buildURL(query)的。并且query是一个对象({ me: true })而不是一个字符串,而buildURL需要modelName作为第一个参数。

所以可能你想做这样的事情:

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

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

  return `${super.buildURL(modelName)}`;
}
于 2020-12-29T14:24:19.060 回答