无法将Node.js中的变量传递给Jade

时间:2015-05-01 13:37:27

标签: node.js express pug

我有几行代码:

var base="http://www.youtube.com/watch?v=";
var videoID=result.items[0].id.videoId;
var link=base+videoID;
console.log(link);

在控制台日志中,我完全看到变量" link"的值。 当我尝试在Jade中使用相同的变量时,我什么都没得到。

p This is the link of the movie trailer:#{link}

我不知道我做错了什么..我已经使用了这个语法#{}(即使在这个文件中!)它就像一个魅力......

有什么建议吗?

这是完整的代码:

app.js

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require('./config');
var http = require('http');
var movies = require('./routes/movies');
var omdb = require('omdb');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var YouTube = require('youtube-node');

    /* Main Function */

    app.post('/findMovie', function(req, res){

    var headline= req.body.movie;

    /* Imdb API */

    omdb.get({ title: headline}, true, function(err, movie) {
        if(err) {
            return console.error(err);
        }
        if(!movie) {
            return console.log('Movie not found!');
        }
        t=movie.title;
        poster=movie.poster;
        y=movie.year;
        r=movie.imdb.rating;
        d=movie.director;
        a=movie.actors;
        p=movie.plot;

       res.render('./movies/results');
    });

    /* Getting all the info about a video*/
    var youTube = new YouTube();
    youTube.setKey('my_key_api');
    youTube.search(headline, 2, function(error, result) {
      var base="http://www.youtube.com/watch?v=";
      var videoID=result.items[0].id.videoId;
      var link=base+videoID;
      if (error) {
        console.log(error);
      }
      else {  
        console.log(headline);
        console.log(link);
      }
    });
    });

results.jade

extends ../default/layout
block content
div.container
div.grad.mov
div.header.mov
    div
        span Movie
form.movies(action='/findMovie', method='post')
    input(type='text' id="movie" name='movie' placeholder='type the name of  
    the movie')
    input(type="submit" id="search" name="search_movie" value='Search')

    div#ajax   
        h1#mov #{t}
        div#left
            img(src="#{poster}")
        div#right
            p#detail Year of the movie: 
                <span>#{y}</span><br>IMDb user rating: 
                <span>#{r}</span><br>Director: 
                <span>#{d}</span><br>Actors: 
                <span>#{a}</span><br><br>
                <span>The plot: </span>#{p}<br>
        div#player
        p#detail This is the URL:#{link}

1 个答案:

答案 0 :(得分:3)

Jade实际上并没有考虑模板中的全局变量。它希望tposter等成为locals对象提供给它的成员。

h1#mov #{t}
h1#mov #{locals.t}

使用Express,locals可以提供res.locals(以及app.locals):

res.locals.t = movie.title;
res.locals.poster = movie.poster;
res.locals.y = movie.year;
res.locals.r = movie.imdb.rating;
res.locals.d = movie.director;
res.locals.a = movie.actors;
res.locals.p = movie.plot;

以及res.render()的第二个参数:

res.render('./movies/results', {
    t: movie.title,
    poster: movie.poster,
    y: movie.year,
    r: movie.imdb.rating,
    d: movie.director,
    a: movie.actors,
    p: movie.plot
});