node.js - 为什么我的标题没有插入到我的数据库中?
问题描述
我正在尝试将输入表单中的标题插入到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>
谁能发现为什么标题没有被插入我的数据库以及为什么它没有显示?
解决方案
调试的一个选项是 console.log 的 req.body 并搜索您作为标题发送的文本。
我认为标题在 req.body.postText 中,或者您应该在标题输入中添加一个名称标签,这将是您的 req.body 中的名称。
让我知道这是否有帮助!
推荐阅读
- sql-server - 将 .bak 文件还原到 SQL Server 2018 Express 时出现问题
- sql - 尝试通过 SQL 查询将字符串拆分为多个部分
- javascript - 如何在 Firebase Cloud Functions 中删除包含嵌套子节点的子节点?
- swift - 在 imageView 的中心添加标签
- linux - 谁创建线程?程序员、操作系统、编译器还是编程语言?
- excel - Amazon Redshift 身份验证问题
- unity3d - 即使游戏暂停且时间刻度设置为 0,协程仍会运行
- python - 如何使用 AWS CLI 旋转 EMR 集群进行测试
- ms-access-2010 - 将大文件上传到 Microsoft Access
- rust - 如何用 println 打印 syn::Expr 的内容?