请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师好,请教一个问题

import 'reflect-metadata'
import { Request, Response } from 'express'
import { controller, get } from "./Decorator";
import { getResponseData } from '../utils/utils';

interface BodyRequest extends Request {
  body: {
    [key: string]: string | undefined
  }
}

@controller
class LoginController {
  // @get('/logout')
  logout(req: BodyRequest, res: Response) {
    if (req.session) {
      req.session.login = undefined
    }
    res.json(getResponseData(true))
  }

  @get('/')
  home(req: BodyRequest, res: Response) {
    const isLogin = req.session ? req.session.login : undefined
    if (isLogin) {
      res.send(`
        <html>
          <body>
            <a href="/getData">爬取内容</a>
            <a href="/showData">展示内容</a>
            <a href="/logout">退出</a>
          </body>
        </html>
      `)
    } else {
      res.send(`
        <html>
          <body>
            <form method="post" action="/login">
              <input type="password" name="password" />
              <button>登陆</button>
            </form>
          </body>
        </html>
      `)
    }
  }
}

老师,login方法并没有使用装饰器,为什么还会打印出undefined

正在回答

1回答

login方法没看见啊,我看见了/和logout

0 回复 有任何疑惑可以回复我~
  • 提问者 目訫 #1
    不好意思.,老师,我说的就是logout,说错了,不过现在明白了,因为去取的时候,logout方法上并没有源数据,所以是undefined
    回复 有任何疑惑可以回复我~ 2020-11-07 16:09:45
  • Dell 回复 提问者 目訫 #2
    好的!
    回复 有任何疑惑可以回复我~ 2020-11-07 16:18:07
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号