本文介绍了带有express 3的dustjs-linkedin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使dustjs-linkedin与Express 3项目一起工作,但是我似乎无法克服此错误:

Error: Template name parameter cannot be undefined when calling dust.compile
at Object.compiler.compile (/home/user/project/node_modules/dustjs-linkedin/lib/compiler.js:21:16)
at Object.dust.compileFn (/home/user/project/node_modules/dustjs-linkedin/lib/dust.js:109:37)
at Function.exports.dust.render (/home/user/project/node_modules/consolidate/lib/consolidate.js:226:56)
at /home/user/project/node_modules/consolidate/lib/consolidate.js:146:25
at /home/user/project/node_modules/consolidate/lib/consolidate.js:99:5
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)

我可以使用全新的express 3项目重现此内容,如下所示:

app.js

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// added these 3
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
app.engine('dust', cons.dust);


app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));

// added this 1
app.set('view engine', 'dust');

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

package.js

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.7",
    "dustjs-linkedin": "*",
    "consolidate": "*"
  }
}

routes/index.js

exports.index = function(req, res){
  res.render('index', { title: 'Express' });

视图/索引.灰尘

Title: {title}

奇怪的是,我有一个使用express 3和dustjs-linkedin的项目运行正常.我对dustjs-linkedin模块中的文件进行了比较,工作版本的dist中有很多文件,而引发错误的项目只有几个,即:

工作/node_modules/dustjs-linkedin/dist:

dust-core.js  dust-core.min.js  dust-full.js  dust-full.min.js  LICENSE

workingProject /node_modules/dustjs-linkedin/dist:

dust-core-1.1.1.js      dust-core-2.0.0.min.js  dust-full-1.2.0.js      dust-full-2.0.1.js
dust-core-1.2.0.js      dust-core-2.0.1.js      dust-full-1.2.1.js      dust-full-2.0.2.js
dust-core-1.2.1.js      dust-core-2.0.2.js      dust-full-1.2.2.js      dust-full-2.0.3.js
dust-core-1.2.2.js      dust-core-2.0.3.js      dust-full-1.2.2.min.js  dust-full-2.1.0.js
dust-core-1.2.2.min.js  dust-core-2.1.0.js      dust-full-1.2.3.js      dust-full-2.2.0.js
dust-core-1.2.3.js      dust-core-2.2.0.js      dust-full-1.2.3.min.js  dust-full-2.2.2.js
dust-core-1.2.3.min.js  dust-core-2.2.2.js      dust-full-1.2.4.js      dust-full-2.2.2.min.js
dust-core-1.2.4.js      dust-core-2.2.2.min.js  dust-full-1.2.5.js      dust-full-2.2.3.js
dust-core-1.2.5.js      dust-core-2.2.3.js      dust-full-1.2.6.js      dust-full-2.2.3.min.js
dust-core-1.2.6.js      dust-core-2.2.3.min.js  dust-full-2.0.0.js
dust-core-2.0.0.js      dust-full-1.1.1.js      dust-full-2.0.0.min.js

这个工作中"项目的演示给了我同样的错误: https://github.com/chovy/express-template-demo

解决方案

此处讨论了一个问题:

https://github.com/linkedin/dustjs/commit/e5ebff0f7b32f8ff0883be7f7924507a314d /p>

[/node_modules/]consolidate/lib/consolidate.js中,转到exports.dust.render功能:

...
try {
  var tmpl = cache(options) || cache(options, engine.compileFn(str));
  tmpl(options, fn);
} catch (err) {
  fn(err);
}
...

engine.compileFn被称为发送str作为参数,str实际上是模板本身:

Title: {title}

engine.compileFn[/node_modules/]dustjs-linkedin/lib/compiler.js中的compiler.compile(source, name).

source是发送的模板,但是从未设置name(即模板名称,在我们的情况下应为index)(未定义).

或者,我更喜欢将Payapl的Adaro库用于express 3.xlinkedin-dustjs.这是他们的项目 kraken.js 的一部分,但可以用作快递的独立除尘器:

https://github.com/paypal/adaro

如README.md中所述:

var express = require('express');
var dustjs = require('adaro');

var app = express();

app.engine('dust', dustjs.dust({});
app.set('view engine', 'dust');

// For rendering precompiled templates:
// app.engine('js', dustjs.js({ ... ));
// app.set('view engine', 'js');

I am trying to get dustjs-linkedin working with an express 3 project, however I can't seem to get past this error:

Error: Template name parameter cannot be undefined when calling dust.compile
at Object.compiler.compile (/home/user/project/node_modules/dustjs-linkedin/lib/compiler.js:21:16)
at Object.dust.compileFn (/home/user/project/node_modules/dustjs-linkedin/lib/dust.js:109:37)
at Function.exports.dust.render (/home/user/project/node_modules/consolidate/lib/consolidate.js:226:56)
at /home/user/project/node_modules/consolidate/lib/consolidate.js:146:25
at /home/user/project/node_modules/consolidate/lib/consolidate.js:99:5
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)

I can reproduce this with a brand new express 3 project like so:

app.js

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// added these 3
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
app.engine('dust', cons.dust);


app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));

// added this 1
app.set('view engine', 'dust');

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

package.js

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.7",
    "dustjs-linkedin": "*",
    "consolidate": "*"
  }
}

routes/index.js

exports.index = function(req, res){
  res.render('index', { title: 'Express' });

views/index.dust

Title: {title}

The strange thing is, I have a project using express 3 and dustjs-linkedin that is working just fine. I ran a diff on the files in the dustjs-linkedin module and the working version has a lot of files in dist while the project throwing the error only has a few, namely:

working/node_modules/dustjs-linkedin/dist:

dust-core.js  dust-core.min.js  dust-full.js  dust-full.min.js  LICENSE

workingProject/node_modules/dustjs-linkedin/dist:

dust-core-1.1.1.js      dust-core-2.0.0.min.js  dust-full-1.2.0.js      dust-full-2.0.1.js
dust-core-1.2.0.js      dust-core-2.0.1.js      dust-full-1.2.1.js      dust-full-2.0.2.js
dust-core-1.2.1.js      dust-core-2.0.2.js      dust-full-1.2.2.js      dust-full-2.0.3.js
dust-core-1.2.2.js      dust-core-2.0.3.js      dust-full-1.2.2.min.js  dust-full-2.1.0.js
dust-core-1.2.2.min.js  dust-core-2.1.0.js      dust-full-1.2.3.js      dust-full-2.2.0.js
dust-core-1.2.3.js      dust-core-2.2.0.js      dust-full-1.2.3.min.js  dust-full-2.2.2.js
dust-core-1.2.3.min.js  dust-core-2.2.2.js      dust-full-1.2.4.js      dust-full-2.2.2.min.js
dust-core-1.2.4.js      dust-core-2.2.2.min.js  dust-full-1.2.5.js      dust-full-2.2.3.js
dust-core-1.2.5.js      dust-core-2.2.3.js      dust-full-1.2.6.js      dust-full-2.2.3.min.js
dust-core-1.2.6.js      dust-core-2.2.3.min.js  dust-full-2.0.0.js
dust-core-2.0.0.js      dust-full-1.1.1.js      dust-full-2.0.0.min.js

This demo of a 'working' project gives me the same error:https://github.com/chovy/express-template-demo

解决方案

[Edit]

An issue is discussed here:

https://github.com/linkedin/dustjs/commit/e5ebff0f7b32f8ff0883be7f7924507b314eef1d

In [/node_modules/]consolidate/lib/consolidate.js go to exports.dust.render function:

...
try {
  var tmpl = cache(options) || cache(options, engine.compileFn(str));
  tmpl(options, fn);
} catch (err) {
  fn(err);
}
...

engine.compileFn is called sending str as parameter, str is actually the template itself:

Title: {title}

engine.compileFn is compiler.compile(source, name) in [/node_modules/]dustjs-linkedin/lib/compiler.js.

source is template sent, however name which is the template name and should be index in our case, is never set (undefined).

Alternatively, I prefer to use Payapl's Adaro library for express 3.x and linkedin-dustjs. It's part of their project kraken.js, but can be used as a standalone dust wrapper for express:

https://github.com/paypal/adaro

As mentioned in README.md:

var express = require('express');
var dustjs = require('adaro');

var app = express();

app.engine('dust', dustjs.dust({});
app.set('view engine', 'dust');

// For rendering precompiled templates:
// app.engine('js', dustjs.js({ ... ));
// app.set('view engine', 'js');

这篇关于带有express 3的dustjs-linkedin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:46