英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论