gpt4 book ai didi

javascript - ember-simple-auth 的 Ember cli 错误

转载 作者:行者123 更新时间:2023-11-29 15:36:34 26 4
gpt4 key购买 nike

我正在尝试将 ember-simple-auth 与自定义 session 一起使用。登录后,当我尝试访问模板中的 session 时,如下所示:

{{session.current_user.email}}

我收到以下错误:

Uncaught Error: Assertion Failed: Required contextualElement for view <Ember._HandlebarsBoundView:ember375> is missing

如果我重新加载页面,错误就会消失。另外,如果我使用未绑定(bind)的助手,它就会消失:

{{unbound session.current_user.email}}

我有以下代码在 user_id 更改时设置 current_user:

import Session from 'simple-auth/session';
import Ember from 'ember';

export function initialize(container) {
Session.reopen({
setCurrentUser: function() {
var id = this.get('user_id');
var _this = this;
if(!Ember.isEmpty(id)) {
return container.lookup('store:main').find('user', id).then(function(user){
_this.set('current_user', user);
});
}
}.observes('user_id')
});
}

export default {
name: 'authentication',
before: 'simple-auth',
initialize: initialize
};

我做错了什么?

编辑#1:

我应该把这段代码放在哪里?初始化程序? app/sessions/custom.js?

export default Session.extend({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return this.container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});

然后在environment.js中我设置:

ENV['simple-auth'] = {
session: 'session:custom'
}

然后在初始化程序中注册自定义 session ?

container.register('session:custom', Session);

编辑#2:

将自定义 session 移至 sessions/custom.js。仍然是同样的错误:

Cryptic Ember Error

最佳答案

我会在 Session 上定义一个 currentUser 方法,将用户作为 promise 返回:

export default Session.extend({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return this.container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});

此外,您最好定义自己的客户 session 类,该类从 Ember Simple Auth Session 类扩展,而不是重新打开它。

关于javascript - ember-simple-auth 的 Ember cli 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27947274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com