为什么我可以添加一个新主题,但我可以't向SQLite数据库添加新注释

Why can I add a new topic but I can't add new comments to the SQLite database?

本文关键字:添加 我可以 SQLite 数据库 注释 新注释 一个 新主题 为什么      更新时间:2023-10-31

我正在开发一个论坛应用程序,我希望用户能够根据每个主题发表自己的评论。因此,我可以创建、发布和显示新的主题,但当我尝试创建新的评论时,它会发布,但除了我在模式文件中的两个评论外,不会显示。我花了整整一周的时间试图解决这个问题,现在是我需要一些认真帮助的时候了。

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数据箭头将通过路由器传递,您可以使用它将注释添加到当前设置的数据库中。