简体   繁体   English

将 store.queryRecord 与 Ember Octane 一起使用时出现错误“str.replace 不是函数”

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

I'm following an Embercasts course (Ember + Rails).我正在学习 Embercasts课程(Ember + Rails)。 For the screencasts, they used Ember 3.0, but I'm using Octane.对于截屏视频,他们使用了 Ember 3.0,但我使用的是 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:在从路由调用的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)

The relevant line is相关线路是

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

This is the UserAdapter :这是用户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?这里有什么问题?

when you do super.buildURL(...arguments) you essentially do super.buildURL(query) .当您执行super.buildURL(...arguments)时,您实际上执行的是super.buildURL(query) And query is an object ( { me: true } ) not a string while buildURL expects the modelName as first parameter.并且query是 object ( { me: true } ) 不是字符串,而buildURL需要modelName作为第一个参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM