我正在尝试实现一个通过 jwt 令牌进行身份验证的 Express API,并且我正在使用 express-jwt 中间件来验证对方法的访问:
import express from 'express'
import jwt from 'express-jwt'
const app = express()
app.get('/protected', jwt({ secret: 'jwtSecret', algorithms: ['HS256'] }), prot)
const prot = (
req: express.Request,
res: express.Response,
_next: express.NextFunction
): void => {
res.json({
msg: req.user,
})
}
app.listen(3000)
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.ts(2339)
我尝试过附加req: express.Request & { user: unknown },但该功能不再适合express。
有什么想法我可以在这里做什么。如果情况变得更糟,我可以告诉 TypeScript 以某种方式闭嘴吗,因为我知道该字段将在那里(即使我知道这不是 TypeScript 的重点)
我有一个对象数组,我正在使用 async forEach 循环遍历该数组,并使用 Axios 发出 HTTP get 请求。我告诉编译器在继续之前等待 axios 完成,但由于某种原因 console.log(data) 仍然在 console.log(ret) 之前运行
我认为这可能是因为 forEach 循环在遇到等待并继续时就被跳过了,但我不知道如何解决这个问题
data.forEach(async (e, i) => {
let req = `https://api.darksky.net/forecast/7e12d816d7818a03901fa6a72e6802f5/${e.lat},${e.log},${Math.floor(e.start_time / 1000)}?units=si`
let ret = await axios(req)
console.log(ret)
data[i]['weather'] = ret.data.currently.summary
data[i]['cloudCover'] = ret.data.currently.cloudCover
})
console.log(data)
Run Code Online (Sandbox Code Playgroud)
这是我看到的输出(请注意,第一个数组理论上应该具有 'weather' 和 'cloudCover' 属性,因为它们是附加的)
[ { start_time: 1548952405372,
end_time: 1548953096266,
lat: 59.57644286,
log: 20.16817143 },
{ start_time: 1548958463054,
end_time: 1548959597889,
lat: 59.57644286,
log: 20.16817143 },
{ start_time: 1548964774667,
end_time: 1548966048587,
lat: 59.57644286,
log: 20.16817143 } ] …Run Code Online (Sandbox Code Playgroud) node.js ×2
async-await ×1
express ×1
express-jwt ×1
javascript ×1
jwt ×1
promise ×1
typescript ×1