Javascript “错误”;str.replace不是一个函数;使用store.queryRecord和Ember辛烷值时
我正在学习一门课程(余烬+轨道)。对于屏幕播放,他们使用了Ember 3.0,但我使用的是Octane 在一个视频中,实现了自定义服务。我的版本是这样的:Javascript “错误”;str.replace不是一个函数;使用store.queryRecord和Ember辛烷值时,javascript,ember.js,ember-simple-auth,ember-octane,Javascript,Ember.js,Ember Simple Auth,Ember Octane,我正在学习一门课程(余烬+轨道)。对于屏幕播放,他们使用了Ember 3.0,但我使用的是Octane 在一个视频中,实现了自定义服务。我的版本是这样的: import Service, { inject as service } from '@ember/service'; export default class CurrentUserService extends Service { @service store; load() { this.store.queryRe
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());
这是用户适配器
:
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(…参数)
时,您实际上执行了super.buildURL(查询)
。而query
是一个对象({me:true}
)而不是字符串,buildURL
需要modelName
作为第一个参数
所以你可能想做类似的事情:
urlForQueryRecord(query, modelName) {
if (query.me) {
delete query.me;
return `${super.buildURL(modelName)}/me`;
}
return `${super.buildURL(modelName)}`;
}
你的适配器看起来怎么样?我假设
str
不是字符串。是的,str
未定义。我在问题中添加了UserAdapter
。