在TypeScript中抛出错误时遇到循环依赖错误。

huangapple go评论50阅读模式
英文:

getting circular dependency error when I am throwing a error in typeScript

问题

以下是翻译好的内容:

在以下代码中,我遇到了一个错误:

> TypeError: 将循环结构转换为JSON
> --> 问题始于使用 'Socket' 构造函数创建的对象。

当我尝试抛出错误消息时发生了错误。具体来说,在执行 "if (!group) throw Error" 这一行时出现了错误。

有人可以帮助我修复这个错误吗?我在这里使用 TypeScript。

新的情况,我遇到了相同的错误:

  async runFilter(request: Request, response: Response, next: NextFunction) {

        return response.status(200).json({})

}
英文:

I encountered an error in the following code:

> TypeError: Converting circular structure to JSON
> --> The issue begins with an object created using the 'Socket' constructor.

The error occurred when I attempted to throw an error message. Specifically, the error appeared when executing the line "if(!group) throw Error".

Can someone help me fixing this error? I am using typescript here.

  async updateGroup(request: Request, response: Response, next: NextFunction) {
    try {
      const { body: params } = request
      const group = await this.groupRepository.findOne(params.id)
  
      if (!group) {
        throw new Error("Group not found")
      }
  
      const updateGroupInput: UpdateGroupInput = {
        id: params.id,
        name: params.name
      }
  
      group.updateConstructor(updateGroupInput)
      const updatedGroup = await this.groupRepository.save(group)
      return response.status(200).json(updatedGroup)
    } catch (error) {
      return response.status(500).json({ message: "Internal server error", error })
    }
  }

New Case where I am getting the same error:


  async runFilter(request: Request, response: Response, next: NextFunction) {

        return response.status(200).json({})

}

答案1

得分: 1

不能直接将socket转换为JSON。必须使用replacer参数排除几个字段,并在replacer函数中返回undefined

JSON.stringify(value, replacer)

找出包含socket对象的字段,将其从replacer中排除。然后以正确的内容类型直接返回JSON字符串。

英文:

You cannot convert a socket to JSON directly. There are several fields that must be omitted with the replacer argument, by keying on those fields and returning undefined from the replacer function.

JSON.stringify(value, replacer)

Figure out what field contains the socket object, and exclude it from your replacer. Then return your JSON string directly with the proper content type.

huangapple
  • 本文由 发表于 2023年5月14日 23:44:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定