简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z 发送到客户端后无法设置标头

[英]Mongoose Cannot set headers after they are sent to the client

I have this Mongoose/Express code that lets you update a document from the logged in user.我有这个 Mongoose/Express 代码,可让您更新登录用户的文档。 I am getting an error: node:events:368 throw er;我收到一个错误:node:events:368 throw er; // Unhandled 'error' event ^ // 未处理的“错误”事件 ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头

This is my code.这是我的代码。

router.post("/update/:id", checkAuth, (req, res) => {
        // check if the snippet was created by the user who is logged in and update the snippet with the new data from the request body
        Snippet.findOne({
            _id: req.params.id,
            userId: req.userData.userId,
        })
            .then((snippet) => {
                if (!snippet) {
                    return res.status(400).json({
                        message: "You don't have a snippet with this ID",
                    });
                }
    
                if (req.body.title) {
                    console.log("if statement");
                    Snippet.findOne(
                        {
                            title: req.body.title,
                            userId: req.userData.userId,
                        },
                        (titleCheckErr, titleCheckSnippet) => {
                            if (titleCheckErr) {
                                return res.status(500).json({
                                    error: titleCheckErr,
                                });
                            }
                            if (titleCheckSnippet) {
                                return res.status(400).json({
                                    message:
                                        "You already have a snippet with that title",
                                });
                            }
                        }
                    );
    
                    snippet.title = req.body.title;
                }
                snippet.description = req.body.description;
                snippet.code = req.body.code;
                snippet.language = req.body.language;
    
                // save the snippet
                snippet.save((saveSnippetErr, saveSnippet) => {
                    if (saveSnippetErr) {
                        return res.status(500).json({
                            error: saveSnippetErr,
                        });
                    }
                    return res.status(201).json({
                        message: "Snippet updated",
                        snippet: saveSnippet,
                    });
                });
            })
            .catch((err) => {
                return res.status(500).json({
                    error: err,
                });
            });
    });

I figured it out.我想到了。 I was making it too complicated.我把它弄得太复杂了。 I simply changed it from using mongoose to check if the title exists, and made it a simple if statement that kept the title the same from db if there was no request body title.我只是将它从使用 mongoose 更改为检查标题是否存在,并使其成为一个简单的 if 语句,如果没有请求正文标题,则从 db 中保持标题相同。 Here's the code.这是代码。

router.post("/update/:id", checkAuth, (req, res) => {
    // check if snippet was created by the user who is logged in, and update the snippet with new data from the request body
    Snippet.findOne(
        { _id: req.params.id, userId: req.userData.userId },
        (err, snippet) => {
            if (err) {
                return res.status(500).json({
                    error: err,
                });
            }
            if (!snippet) {
                return res.status(400).json({
                    message: "Snippet not found",
                });
            }
            if (req.body.title) {
                snippet.title = req.body.title;
            } else {
                snippet.title = snippet.title;
            }
            snippet.description = req.body.description;
            snippet.code = req.body.code;
            snippet.language = req.body.language;

            // save the snippet
            snippet.save((saveErr, saveSnippet) => {
                if (saveErr) {
                    return res.status(500).json({
                        error: saveErr,
                    });
                }
                console.log("updated");
                return res.status(201).json({
                    message: "Snippet updated",
                    snippet: saveSnippet,
                });
            });
        }
    );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用猫鼬和node.js将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client with mongoose and node.js 使用 mongoose.removeOne 后,Node JS throwing 在发送到客户端后无法设置标头 - Node JS throwing cannot set headers after they are sent to the client, after using mongoose.removeOne 将标头发送到生产环境中的客户端后无法设置标头 - Cannot set headers after they are sent to the client in production NodeJS-将标头发送到客户端后无法设置标头 - NodeJS - Cannot set Headers after they are sent to the client 使用 postman 将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client with postman 使用express.js将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client with expressjs 以快递方式发送给客户端后无法设置标头 - Cannot set headers after they are sent to the client in express 在中间件中将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client in a middleware 发送到客户端 Express 后无法设置标头 - Cannot set headers after they are sent to the client Express 将标头发送到客户端后无法设置标头 - 错误 - Cannot set headers after they are sent to the client - error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM