NodeJS 此表达式不可调用,类型“Number”没有调用签名

8ljdwjyq  于 12个月前  发布在  Node.js
关注(0)|答案(6)|浏览(242)

嗨,我是新的Typescript,我有一个对象类型变量,其中可能是不同类型或嵌套对象的不同值。现在我的问题是,我如何定义一个模型,使这个对象在调用不同的键时不会遇到示例错误?
举例来说:

export class Controller {
protected static response(res: Response, statusCode: number = 200, data: any, user: string = '', dev: string = '', code: number = 200, result: string = 'success'){
    res.status(statusCode).send({
        data: data,
        message: {
            user: '',
            dev: ''
        },
        code: 403,
        result: 'Error'
    })
}

 ERROR: res.status ---> This expression is not callable. Type 'Number' has no call signatures
xiozqbni

xiozqbni1#

我也得到了这个错误,并意识到我只是忘记了导入响应。添加一个导入行为我解决了这个问题。

import express, {Request, Response} from 'express';
im9ewurl

im9ewurl2#

在NextJS中,请确保导入正确的响应类型。

import { NextApiResponse } from "next";
23c0lvtd

23c0lvtd3#

为了帮助其他人,我也在这样的情况下看到了同样的错误:

const shift = progressWidth * percentage
(this.$refs.expected as Vue).$el.style.left = "100px"

其中percentagenumber。发生错误是因为没有使用通配符,第二行被解释为该行的一部分,如下所示:

const shift = progressWidth * percentage(this.$refs.expected as Vue).$el.style.left = "100px"

这可以通过添加一个前导分号来解决:

const shift = progressWidth * percentage
;(this.$refs.expected as Vue).$el.style.left = "100px"

或者更好的是,重新排列,这样它就不需要了:

const expected = (this.$refs.expected as Vue).$el
const shift = progressWidth * percentage
expected.$el.style.left = "100px"
z5btuh9x

z5btuh9x4#

在你的contoller中,只需要从express或者你使用的框架中导入响应,它就会工作。

import { Response } from 'express';
4szc88ey

4szc88ey5#

res.status是根据该错误消息的数字。看起来Controller没有被正确的参数调用。console.log(res)在那里之前调用res.status和检查您的调用站点代码.

wbgh16ku

wbgh16ku6#

首先添加这一行,以获取NEXTJS中express的所有功能。请记住,你添加了这一确切的行,而不是其他人在这一页中提到。

import { NextApiRequest, NextApiResponse } from "next"; 

// This function handle the POST request and defeat all your errors.
import { NextApiRequest, NextApiResponse } from "next";

// You must have to mention the type of handle function's parameter which we already
//get from next package (NextApiRequest, NextApiResponse). 
//NOTE:- You can change the function name, that's your choice

export default function handle(req: NextApiRequest, res: NextApiResponse) {
    if (req.method !== "POST") {
        res.status(405)
    }
}

相关问题