【发布时间】:2026-02-17 08:45:02
【问题描述】:
我对 typescript 相当陌生,并试图让 Express-server 运行它。所以我基本上是想在用 axios 获取数据后让下面的代码运行:
import express, { Response, Request } from 'express'
(...)
app.get('/api/mk', async (req: express.Request, res: express.Response) => {
await axios
.get('http://localhost:8080/rest/kuk/v1/ownerMKKOL/getALL')
.then((axiosResponse) => {
const { data } = axiosResponse
return res.status(200).send(data)
})
.catch((err) => err)
})
但是,我收到以下错误消息,并且无法理解它:
此表达式不可调用。 “号码”类型没有呼叫签名。 TS2349
我必须扩展Response-接口吗?如果是这样,可能的解决方案是什么样的?
非常感谢任何帮助!
解决方案:
我终于找到了基本上归结为我的项目结构问题的解决方案:
Root
|-- React App
|-- Server
| |-- package.json
| `-- ...
`-- package.json
`-- ...
不知何故,当从我的React App 包运行构建命令时,会出现这个问题。我确实通过以下方式重组了我的项目,问题确实消失了:
Root
|-- Server
| |-- package.json
| |-- express.ts
| `-- ...
`-- Client
|-- package.json
`-- ...
【问题讨论】:
-
我更关心你在同一个承诺链中同时使用
await和.then,显然是没有理由的。另外,如果您要通过调用 express.Request 等来使用它的属性,为什么还要从 express 导入响应和请求 -
顺便说一句,你可以跳过
.status(200)(默认设置)。
标签: javascript node.js typescript express