为什么设置不了session

这个是登录的controller

async indexAction() {
 if (this.isGet()) {
 return this.display();
 }
 var postData = this.post();
 if (postData.username === 'admin' && postData.password === '123') {
 await this.session('userInfo', {
 username: 'admin'
 });
 return this.redirect('/admin');
 }
 this.assign({
 msg: "用户名或密码不正确"
 });
 return this.display();
 };

这个是取session的controller

async indexAction() {
 //auto render template file index_index.html
 // if(!this.session())
 console.log(await this.session('userInfo').username);
 return this.display();
 };

写法不对还是?

w3ctech微信

扫码关注w3ctech微信公众号

共收到3条回复

  • 俺的是:

    let user_info = await this.session('user_info');
    if (think.isEmpty(user_info)) {
     return this.redirect('/admin/login');
    }
    

    表示可以用

    回复此楼
  • console.log(await this.session('userInfo').username); 这个写法不对

    console.log((await this.session('userInfo')).username); 这样应该就可以了

    回复此楼
  • @老六 请教await写法是为了避免异步函数无限嵌套下去,像这种就打印获取到的一条信息 前面是不是就不用写await了 还有就是* yield 写法和async await写法 在性能上或其他方面有差别吗

    回复此楼

AltStyle によって変換されたページ (->オリジナル) /