Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t发送后设置标题-Node.js重定向问题与表单_Node.js - Fatal编程技术网

Can';t发送后设置标题-Node.js重定向问题与表单

Can';t发送后设置标题-Node.js重定向问题与表单,node.js,Node.js,我有一张表格,可以向路线发出post请求。它接受一些输入并保存到数据库中。然后,在成功保存后,我重定向到我所在的原始页面。我的代码如下: router.post("/save", function (request, response, next) { const metadata = { user: currentUser.name + " " + currentUser.surname date: <some-val> }; for

我有一张表格,可以向路线发出post请求。它接受一些输入并保存到数据库中。然后,在成功保存后,我重定向到我所在的原始页面。我的代码如下:

router.post("/save", function (request, response, next) {
    const metadata = {
        user: currentUser.name + " " + currentUser.surname
        date: <some-val>
    };
form.parse(request, function(err, fields, files){
    if(!fields.name){
        return response.status(400).send({"status": "error", "message": "The name is required" });
    }
    else if(!fields.description){
        return response.status(400).send({"status": "error", "message": "The description is" +
        " required" });
    }
    MediaH.upload(files, metadata, function (error, result) {
        if(error){
            return response.send(error);
        } else {
            Model.save(fields, result, metadata, function (error, result) {
                if(error) {
                    return response.send(error);
                }
                else{
                   return response.redirect("/pages")
                }
            });
        }
    });

});
});
router.post(“/save”,函数(请求、响应、下一步){
常量元数据={
用户:currentUser.name+“”+currentUser.name
日期:
};
解析(请求、函数(错误、字段、文件){
如果(!fields.name){
返回response.status(400).send({“status”:“error”,“message”:“name is required”});
}
else if(!fields.description){
返回response.status(400).send({“status”:“error”,“message”:“描述为”+
“必需”});
}
上传(文件、元数据、函数(错误、结果){
如果(错误){
返回响应。发送(错误);
}否则{
保存(字段、结果、元数据、函数)(错误、结果){
如果(错误){
返回响应。发送(错误);
}
否则{
返回响应。重定向(“/pages”)
}
});
}
});
});
});
重定向成功,但是如果我再次尝试重新上传到我的表单,我会得到一个
错误:发送后无法设置标题。
错误。我不知道哪里出了问题。我看了几个关于堆栈溢出的类似问题,有人建议对
响应执行
返回
。重定向
,但它仍然不起作用

有人能帮忙吗

编辑:


把整个路线放在上面。

你确定在
表单之后没有代码。解析
可能会在响应中发送一些东西吗?嗨@ExplosionPills,我已经做了编辑。“下一步”怎么办?您不应该在response.send()之后调用它吗?像
response.send(400);返回next()