首页 > 解决方案 > 为什么我的主页在发布后不显示?

问题描述

所以我试图index.ejs在发布后重定向回页面newPost.ejs。上传工作,但它永远不会重定向回来。点击后只留下一个持续加载页面Post

我假设我的路线或 ejs 有问题。

这是我的发帖路线:

app.post('/newPost', isLoggedIn, uploads.single('inputFile'), (req, res) => {
  console.log('On POST route');

  // get an input from user
  let file = req.file.path;
  console.log(file);

  
  cloudinary.uploader.upload(file, (result) => {
    console.log(result);
        
    db.post.create({
          caption: req.body.title,
          image_url: result.url,
          userId: req.body.id
        })
        
    // Render result page with image
  }).then((post) => res.render('index', { image: result.url }));
})

这是我的获取路线:

app.get('/', (req, res) => {
  console.log(res.locals.alerts);
  db.post.findAll()
  .then((posts) => {
    const postArray = posts.map(post => {
      return post.get();
    })
    console.log(postArray);    
    res.render('index', { posts: postArray });
  })
});

这是index.ejs

<div>
    <% posts.forEach(function(post) { %>
        <h1></h1>
        <img class="images" width="700px" height="500px" src="<%= post.image_url %>" alt="uploaded image"> 
        <form action="/<%= post.id %>?_method=DELETE" method="POST">
            <input id="deleteButton" class="btn-danger" type="submit" value="Remove idea" >
        </form>
        <br>
    <% }) %>
</div>

最后,这是我的newPost.ejs

<div class="postSection">
    <form action="/" method="POST" enctype="multipart/form-data">
        <input type="title" placeholder="Write a Caption:" id="postText">
        <input type="file" name="inputFile" id="inputFile"> 
        <input type="submit" class="btn btn-primary" id="postSubmit" value="Post">
    </form>
</div>

标签: node.jsdatabaseexpressserverejs

解决方案


推荐阅读