簡體   English   中英

將 store.queryRecord 與 Ember Octane 一起使用時出現錯誤“str.replace 不是函數”

[英]Error “str.replace is not a function” when using store.queryRecord with Ember Octane

我正在學習 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 function 中, 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)}`;
  }
}

這里有什么問題?

當您執行super.buildURL(...arguments)時,您實際上執行的是super.buildURL(query) 並且query是 object ( { me: true } ) 不是字符串,而buildURL需要modelName作為第一個參數。

所以可能你想做這樣的事情:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM