相关疑难解决方法(0)

如何在打字中安装快递?

我想在我的应用程序中使用expressjs.

使用后安装它typings install express --ambient --save,我运行tsc,但我得到两个错误:

typings/main/ambient/express/index.d.ts(17,34):error TS2307:找不到模块'serve-static'.分型/主/环境/快递/ index.d.ts(18,27):错误TS2307:找不到模块"表达发球静态核心".

所以,我试图安装两个:

typings install serve-static --ambient --save
typings install express-serve-static --ambient --save
Run Code Online (Sandbox Code Playgroud)

然后我再次运行tsc,但又得到一个错误:

typings/main/ambient/serve-static/index.d.ts(79,24):error TS2307:找不到模块'mime'.

我该如何解决这些问题?如何自动安装express的所有依赖项?

typescript definitelytyped tsd

16
推荐指数
4
解决办法
2万
查看次数

类型“string |”上不存在属性“_id” JwtPayload'。类型“string”上不存在属性“_id”

正如您在 NodeJS 代码中看到的,我正在从 URL 中提供的 Postman 的令牌中获取用户,但我正在努力从给定的令牌编号中取回 _id 属性。我想要的是已解码的 _id 中的 id,以便我可以在进一步的操作中使用该 id

const jwt = require('jsonwebtoken')
const User = require('../model/users')
const auth = async (req, res, next) => {
    try {
          const token = req.header('Authorization').replace('Bearer', '')
          const decoded = jwt.verify(token, 'thisisfromabhishek')

`the property _id does not exist on decoded`
          const user = await User.findOne({ _id: decoded._id, 'tokens.token': token })

        if (!user) {
            throw new Error()
        }
        req.token = token
        req.user = user
        next()
       } catch (e) {
        res.status(401).send({ error: …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb node.js jwt

12
推荐指数
1
解决办法
1万
查看次数

扩展护照用户参数的快递请求

我正在使用 Passport 处理 Express 应用程序的身份验证。这将用户设置为 Express 响应。我正在使用 TypeScript,因此在路由定义中将请求类型设置为 Request 会出错,因为 Express Request 中不存在用户对象。通过声明合并扩展接口来扩展请求有很多问题,但这些会导致另一个错误。我的文件看起来像这样:

import { Router, Request, Response }  from 'express'
import { User as CustomUser } from './user'

interface IRequest extends Request {
  user: CustomUser
}

const router: Router = Router()

router.get('/', requiresLogin, (req: IRequest, res: Response) => {
  console.log(req.user)
  res.sendStatus(204)
})
Run Code Online (Sandbox Code Playgroud)

但现在我在快速回调中得到以下 TypeScript:

Argument of type '(req: IRequest, res: Response) => void' is not assignable to parameter of type 'RequestHandlerParams'. Type '(req: IRequest, res: …
Run Code Online (Sandbox Code Playgroud)

express typescript passport.js typescript2.0

10
推荐指数
1
解决办法
2171
查看次数