首页 > 解决方案 > 为什么我的标题没有插入到我的数据库中?

问题描述

我正在尝试将输入表单中的标题插入到caption数据库中的列中,然后将其与帖子一起显示。显示图像和删除按钮,但不显示标题。

这是我在 server.js 中的路线:

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 }));
})

这是我的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>

最后,这是我的index.ejs页面,它将在其中显示:

<div>
    <% posts.forEach(function(post) { %>
        <h1><%= post.caption %></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>

谁能发现为什么标题没有被插入我的数据库以及为什么它没有显示?

标签: node.jsdatabaseexpressserverejs

解决方案


调试的一个选项是 console.log 的 req.body 并搜索您作为标题发送的文本。

我认为标题在 req.body.postText 中,或者您应该在标题输入中添加一个名称标签,这将是您的 req.body 中的名称。

让我知道这是否有帮助!


推荐阅读