简体   繁体   English

使用NodeJS,Gulp和Jade:如何在.jade文件中使用本地语言?

[英]Working with NodeJS, Gulp, Jade: how can I use locals in my .jade files?

So I'm trying to build a more or less simple web app with NodeJS, Express, Gulp and Jade for templating for the first time. 因此,我正在尝试使用NodeJS,Express,Gulp和Jade构建或多或少简单的Web应用程序以进行首次模板化。 I did not plan to use Gulp at first but after coding all the core functionalities I bought a template that came with Gulp. 我最初并不打算使用Gulp,但是在对所有核心功能进行编码之后,我购买了Gulp随附的模板。 Without Gulp everything was running fine but I can't figure out how to make it work it with Gulp. 没有Gulp,一切都会运行良好,但是我不知道如何使其与Gulp一起工作。

So here is my problem: 所以这是我的问题:

When I try to create my html files with gulp/gulp-jade I get the following error: 当我尝试使用gulp / gulp-jade创建html文件时,出现以下错误:

TypeError: /Users/root1/doordeals-be/views/admin-coupon-overview.jade:4
2|
3| block content
4|     h1 #{coupon.title}

Cannot read property 'title' of undefined

Here is how coupon is passed to jade in my Express route: 这是在我的Express路线中将coupon传递到翡翠的方法:

router.get('/:couponId', function(req, res, next) {
  Coupon.findById(req.params.couponId).populate('dispatch region').exec(function(err, coupon) {
    if(err) {
        return next(err);
    } else {
        res.render('admin-coupon-overview', {coupon: coupon});
    }
  });
});

Here is my gulp file: 这是我的gulp文件:

gulp.task('serve', ['sass', 'templates'], function () {
browserSync.init({
    port: 3000,
    server: "./",
    index: './views/html/index.html',
    ghostMode: false,
    notify: false
});

gulp.watch('./src/assets/scss/**/*.scss', ['sass']);
gulp.watch('./src/**').on('change', browserSync.reload);

});

gulp.task('templates', function() {
  var YOUR_LOCALS = {};

gulp.src('./views/*.jade')
    .pipe(jade({
  locals: YOUR_LOCALS
  }))
  .pipe(gulp.dest('./views/html'))
});

That's basically it. 基本上就是这样。 So the problem is that coupon gets defined when someone sends a GET request to /:couponId (therefore he has to visit http://my.link.com/SOME-ID ) but Gulp tries to create all the HTML files as soon as the server gets started. 因此,问题在于,当有人向/:couponId发送GET请求(因此他必须访问http://my.link.com/SOME-ID )时, coupon就被定义了,但是Gulp试图尽快创建所有HTML文件。服务器启动。

Does anyone know how to solve this issue? 有谁知道如何解决这个问题?

Thanks in advance. 提前致谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM