使用ember-simple-auth我设置会话并将currentUser键设置为等于从服务器返回的用户对象(编辑:整个session.js文件的内容):
//app/services/session.js
import Ember from 'ember';
import ESASession from "ember-simple-auth/services/session";
export default ESASession.extend({
store: Ember.inject.service(),
session: Ember.inject.service(),
setCurrentUser: function () {
if (this.get('isAuthenticated')) {
const accountId = this.get('session.content.authenticated.user_id');
console.log(accountId);
this.get('store').findRecord('user', accountId).then(user => {
this.set('currentUser', user);
});
}
}.observes('isAuthenticated')
});
问题是当我尝试从数据关系的路由访问'currentUser'键时:
user: Ember.computed(function(){
console.log(this.get('session'));
//return `${this.get('session.currentUser')}`;
}),
在控制台中我看到(除了一堆其他东西):
currentUser: Class
但如果我尝试记录'session.currentUser',我看到未定义。
如何从此路线访问用户ID?这有很多麻烦!提前致谢:D
答案 0 :(得分:3)
尝试
this.get('session.data.currentUser')