为什么我可以添加一个新主题,但我可以't向SQLite数据库添加新注释
Why can I add a new topic but I can't add new comments to the SQLite database?
我正在开发一个论坛应用程序,我希望用户能够根据每个主题发表自己的评论。因此,我可以创建、发布和显示新的主题,但当我尝试创建新的评论时,它会发布,但除了我在模式文件中的两个评论外,不会显示。我花了整整一周的时间试图解决这个问题,现在是我需要一些认真帮助的时候了。
var express = require('express');
var sqlite3 = require('sqlite3');
var fs = require('fs');
var Mustache = require ('mustache');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var db = new sqlite3.Database('./forum.db');
var app = express();
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(methodOverride('_method'));
app.get('/', function(req, res){
res.send(fs.readFileSync('./views/topics/index.html', 'utf8'));
});
app.get('/topics', function(req,res) {
var template = fs.readFileSync('./views/topics/topics.html', 'utf8');
db.all("SELECT * FROM topics;", function(err, topics){
var html = Mustache.render(template, {listoftopics: topics});
res.send(html);
});
});
app.get('/topics/new', function(req, res){
res.send(fs.readFileSync('./views/topics/new.html', 'utf8'));
});
app.post('/topics/new', function(req, res){
console.log(req.body);
db.run("INSERT INTO topics(title, creator, date, body) VALUES ('" + req.body.title + "','" + req.body.creator + "','" + req.body.date + "','" + req.body.body + "')");
res.redirect("/topics")
});
app.get('/comments/new', function(req, res){
res.send(fs.readFileSync('./views/comments/newComment.html', 'utf8'));
});
app.post('/comments/new', function(req, res){
console.log(req.body)
db.run("INSERT INTO comments(person_created, input) VALUES ('" + req.body.person_created + "','" + req.body.input + "')");
res.redirect("/topics/")
});
app.get('/topics/:id', function(req, res){
var id = req.params.id;
res.locals.id = id
db.all("SELECT * FROM topics WHERE id = " + id + ";", {}, function(err, topic){
console.log(topic)
var body = topic.body;
db.all("SELECT * FROM comments WHERE topic_id = " + id + ";", {}, function(err, comment){
var person_created = comment.person_created;
var input = comment.input
fs.readFile('./views/topics/show.html', 'utf8', function(err, html){
var renderedHTML = Mustache.render(html, {body:topic, person_created:comment, input:comment});
res.send(renderedHTML);
console.log(comment);
});
});
});
});
app.listen(3000, function(){
console.log("LISTENING!");
});
我的架构文件
var sqlite3 = require ('sqlite3');
var db = new sqlite3.Database('./forum.db');
db.serialize(function(){
db.run("CREATE TABLE topics(id integer primary key AUTOINCREMENT, title varchar, creator varchar, date varchar, body varchar);")
db.run("CREATE TABLE comments(person_created varchar, input varchar, topic_id integer, FOREIGN KEY (topic_id) references topics(id));")
db.parallelize(function(){
db.run("INSERT INTO topics(title, creator, date, body) VALUES ('Top R&B Hits of the 80s', 'Michael', '4/15/15', 'Please share some of your favorite R&B Hits from the decade!' );")
db.run("INSERT INTO comments(person_created, input, topic_id) VALUES ('Sheila', 'Bille Jean by Michael Jackson', 1);")
db.run("INSERT INTO comments(person_created, input, topic_id) VALUES ('George ', 'Gett Outta of My Dreams by Billy Ocean', 1); ")
})
})
显示新评论的页面
<!DOCTYPE html>
<html lang='en'>
<head>
<style type="text/css">
body{
background-image: url("http://blog.paradizo.com/wp-content/uploads/2010/03/nyc-empire-room.jpg");
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% auto;
}
</style>
<meta charset='UTF-8'>
<title>Topic ID</title>
</head>
<body>
<center>
{{#body}}
<h1>{{body}}</h1>
{{/body}}
<h2>Comments<h2>
<h3>
<ol>
{{#person_created}}
<li>
{{person_created}} - {{input}}
</li>
{{/person_created}}
</ol>
</h3>
<form action="/comments/new" method='GET'>
<button>Create New Comment</button>
</form>
</center>
</body>
</html>
嘿,Mike,我注意到在你的路由注释/new中,你执行了一个sql查询,这可能是个问题。
db.run("INSERT INTO comments(person_created, input) VALUES ('" + req.body.person_created + "','" + req.body.input + "')");
我认为应该是db.run("INSERT INTO comments(person_created, input, topic_id) VALUES ('" + req.body.person_created + "','" + req.body.input + "')");
您的form
应该使用POST
方法提交评论,以便为/comments/new
URL触发正确的router
。您还需要将路由器中所需的input
字段作为表单的一部分提交,以便在req.body
阵列中公开它们。
<form action="/comments/new" method='POST'>
<input type="text" value="person_created">
<input type="text" value="input">
<input type="submit" value="Submit">
</form>
这将允许您在提交表单时正确触发相关路由器。然后req.body
数据箭头将通过路由器传递,您可以使用它将注释添加到当前设置的数据库中。
相关文章:
- 我可以在json对象中添加一个函数吗
- 我可以在Dynamics CRM 2016的复合地址字段中添加两个选项字段吗
- 我可以使用JavaScript闭包添加数字吗
- 我可以限制我添加到数组中的项目数量吗
- 我可以通过javascript在鼠标光标旁边动态添加文本吗?
- Kinetic JS:我可以在形状中添加多个名称吗?
- 添加变换比例后,我可以't将剖面设置为居中
- 我可以用添加css吗!重要的是,只使用jquery
- 为什么我可以't在所需元素中添加一些html
- 我可以用Javascript向数组添加函数吗
- 为什么我可以添加一个新主题,但我可以't向SQLite数据库添加新注释
- 我可以向数组添加对象键吗
- 我可以'我找不到一种将数据库源添加到dgrid的优雅方法
- 我可以使用jquery和AJAX来调用cgi-bin脚本,然后添加消息事件来处理服务器发送事件吗
- 如何添加id和额外的参数到一个放大弹出iframe与youtube视频,所以我可以控制与API youtube播放器
- 我可以在蜘蛛网的高图表中添加两个轴,然后单击x轴标签导航到另一个页面吗
- 我可以在String.prototype中添加一个额外的变量吗
- 我可以使用网络爬虫将新的 youtube 视频添加到播放列表吗?
- 我可以在推特上分享来自不同网站的帖子时添加图片吗?
- 我可以为单个平台添加流星包吗?