Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我可以添加新主题,但不能向SQLite数据库添加新注释?_Javascript_Node.js_Express_Sqlite_Mustache - Fatal编程技术网

Javascript 为什么我可以添加新主题,但不能向SQLite数据库添加新注释?

Javascript 为什么我可以添加新主题,但不能向SQLite数据库添加新注释?,javascript,node.js,express,sqlite,mustache,Javascript,Node.js,Express,Sqlite,Mustache,我正在开发一个论坛应用程序,我希望用户能够针对每个主题发表自己的评论。因此,我可以创建、发布和显示新主题,但当我尝试创建新注释时,它将发布,但除了模式文件中的两条注释外,不会显示。我花了整整一周的时间试图解决这个问题,现在是我需要一些认真帮助的时候了 var express = require('express'); var sqlite3 = require('sqlite3'); var fs = require('fs'); var Mustache = require ('mustach

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

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,我注意到在你的路线注释/新路线中,你执行的sql查询可能就是问题所在

db.runINSERT到commentsperson_created中,输入值“+req.body.person_created+”,“+req.body.input+”

我认为应该是db.runINSERT到commentsperson_created,input,topic_id值“+req.body.person_created+”,“+req.body.input+”

您的表单应该使用POST方法提交评论,以便为/comments/新URL触发适当的路由器。您还需要提交路由器中所需的输入字段,作为表单的一部分,以便在req.body数组中公开这些字段


这将允许您在提交表单时正确触发相关路由器。req.body数据箭头随后将通过路由器,您可以使用它将注释添加到数据库中,因为您目前已经进行了设置。

感谢您指出这一点,但不幸的是,它没有解决问题。您可以发布HTML,包括用于提交注释的表单吗?可以,但不会显示新注释。在我的终端中,它将通过console.log显示输入,但没有主题id。您可以添加用于将表单提交到帖子的HTML吗?添加是什么意思?你是说我可以在html中添加新的评论吗?这是你用来提交评论的吗?创建新评论我检查了我的终端,这是我通过控制台得到的{person_created:'Mike',input:'Test'},但仍然没有发布到HTML。这是我现在拥有的查询app.post'/comments/new',functionreq,res{console.logreq.body db.runINSERT-INTO-commentsperson_-created,input,topic_-id值'+req.body.person_-created+','+req.body.input+','+req.body.topic_-id+';res.redirect/topics/};不起作用,因为它在输入区域内有值的名称,我必须在它们上面键入。我有name='person_created'instead刚在sqlite中检查了我的注释表,它确实添加了新注释,但没有添加到html中。在sqlite中显示:sqlite>SELECT*FROM COMMENTS;Sheila | Billie Jean by Michael Jackson | 1 George | Get Out of My Dreams by Billy Ocean | 1 Mike | Test | undefinedIt看起来您正在为注释设置局部变量:var renderedHTML=mustach.renderhtml,{body:topic,person|created:comment,input:comment};。您可能需要验证注释变量的内容。请注意,您正在将person_created和input设置为与本地变量完全相同的值,以便在HTML中呈现。我不明白这一点。如何验证?
<form action="/comments/new" method='POST'>
   <input type="text" value="person_created">
   <input type="text" value="input">
   <input type="submit" value="Submit">
</form>