【问题标题】:Access Jade table values in Node.JS在 Node.JS 中访问 Jade 表值
【发布时间】:2016-05-26 22:11:00
【问题描述】:
table(class= "table-striped table-striped table-hover")
        thead
                th  
                 h2 #{movie.title}

        tbody

                tr

                    td
                        b Tagline : #{movie.tag} 
                        br
                        b Rating :  #{movie.rating}
                        br
                        b Synopsis :
                        p #{movie.overview}
                        b Available Copies : #{movie.copies}
                        br
                        input.btn-danger.btn-sm(type="submit", value="Buy")  

                    td
                         img(src= movie.poster width="250" height="300")
                         p
                            a.btn.btn-info(href='http://www.imdb.com/title/'+movie.movie_id)  Watch Trailer »

#{ } 值来自 Mysql 表。提交此页面后,我想重用 #{movie.title} #{movie.tag} 在 Node.js 控制台中。比如我想做console.log(movie.title) 我该怎么做?任何帮助将不胜感激。

【问题讨论】:

  • 我假设您有一个渲染此文件的 Node.js 应用程序?如果是这样,您不能在渲染之前/之后简单地记录数据吗?
  • 提交页面后如何记录数据?

标签: javascript mysql node.js express pug


【解决方案1】:

使用 jquery 提交,并将 #{movie.title}#{movie.tag} 添加到帖子的变量中,就像这样..

按钮:

<input type='button' value='Submit form' onClick='submitDetailsForm()' />

JS:

   <script language="javascript" type="text/javascript">
    var title = !{
        JSON.stringify(movie.title)
    };
    var tag = !{
        JSON.stringify(movie.tag)
    };
    var data = {
        title: title,
        tag: tag
    };

    function submitDetailsForm() {
        $.ajax({
            type: 'POST',
            data: JSON.stringify(data),
            cache: false,
            contentType: 'application/json',
            url: '/send',
            success: function(retorno) {

            }
        });
    }
</script>

Node.js:

app.post('/send', function(req, res) {
    console.log(req.title);
    console.log(req.tag);
});

【讨论】:

    【解决方案2】:

    这可能不是一种有效的方法。尝试使用global 对象。

     global.movieName = rows[0].title;
     global.movieRating = rows[0].rating;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-15
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多