How can I render and redirect at the same time error: Cannot set headers after they are send to the

编程入门 行业动态 更新时间:2024-10-06 16:25:13

How can I render and redirect at the same time error: Cannot set headers after they are send to the client <a href=https://www.elefans.com/category/jswz/34/1771449.html style=错误"/>

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

本文发布于:2024-05-30 14:36:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   time   error   render   redirect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!