错误"/>
How can I render and redirect at the same time error: Cannot set headers after they are send to the client 错误
我正在开发一个植物社区项目。所以我想做的是有两个部分,正如您在第一部分中看到的那样,我有一个图像和一个按钮,下面写着 ai。所以当我点击按钮时,我需要发送一个获取请求,以便它可以分析图像并在右侧给我结果。但问题是,我通过将操作设置为 /ai/search 在 ejs 上使用 Fetch 请求。我也在使用端点来听取该请求 ejs
<form action="/ai/search/<%= posts[i]._id%>" method="get">
<button type="submit">AI</button>
</form>
控制器
module.exports = {
getImageResult: async (req, res) => {
const post = await Post.findById(req.params.id);
const result = await fetch(
`://my-api.plantnet/v2/identify/all?images=https%3A%2F%2Fi.pinimg%2Foriginals%2Fe1%2F65%2Fbf%2Fe165bfa6677b80c0a0416ecb386f4caf.jpg&include-related-images=false&no-reject=false&lang=en&api-key=2b10n7zULKkcjhMWnZmdjbS4te`
);
const data = await result.json();
res.render("./partials/commentSection.ejs", { ai: data },);
res.redirect("/feed")
},
};
所以它将被重定向。到那个端点,但我不想重定向它。所以当我得到结果时,我将它发送回这个 AI,但我不能同时重定向和渲染。我能做些什么? 图片
回答如下:更多推荐
How can I render and redirect at the same time error: Cannot set headers after t
发布评论