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

Can't pass a variable from Node.js to Jade

我有这几行代码:

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}

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

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

使用Express,locals可以提供res.locals (and 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
});