为什么我可以添加新主题但我无法向SQLite数据库添加新注释?

时间:2016-04-01 05:17:36

标签: javascript node.js express sqlite mustache

我正在开发论坛应用,我希望用户能够针对每个主题发布自己的评论。所以我已经能够创建,发布和显示新主题,但是当我尝试创建新注释时,它将发布但不会显示,除了我在模式文件中的两条注释。我花了一周的时间试图解决这个问题,现在是我需要一些认真帮助的时候了。

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>

2 个答案:

答案 0 :(得分:0)

嘿Mike我注意到你的路线评论/新你执行一个可能是问题的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 + "')");

答案 1 :(得分:0)

您的form应使用POST方法提交评论,以便为router网址触发正确的/comments/new。您还需要在路由器中提交所需的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数据箭头将通过路由器传递,您可以使用它将注释添加到您正在设置的数据库中。