Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array for views gives "TypeError: Arguments to path.resolve must be strings" #124

Open
sundarj opened this issue Apr 17, 2015 · 4 comments · May be fixed by #126
Open

Array for views gives "TypeError: Arguments to path.resolve must be strings" #124

sundarj opened this issue Apr 17, 2015 · 4 comments · May be fixed by #126

Comments

@sundarj
Copy link

sundarj commented Apr 17, 2015

Hi, I have express-handlebars 2.0.0 on express 4.12.3, and I think I've discovered a bug.

The aforementioned TypeError gets raised when I try and render a view. The line that seems to be causing it is line 193 of express-handlebars.js: view = this._getTemplateName(path.relative(viewsPath, viewPath)). Since I've passed an array to app.set('views'), viewsPath is also an array, and thus raises that error. I'd try and fix it myself, but I don't really know how I'd go about that.

Just in case it helps, here is my index.js (the error occurs when I visit /resonate/bts/editor):

var express = require('express');
var path = require('path');
var fs = require('fs');
var hbs = require('express-handlebars');
var app = express();

app.set('view options', {layout: false});
app.engine('.rsn', hbs({extname: '.rsn'}));
app.set('view engine', '.rsn');
app.set('views', [
  path.join(__dirname, 'resonate/bts',
  path.join(__dirname, 'resonate/template')
]);

app.enable('trust proxy');

...

var files = [];
walk('resonate/template', function(f,s) {
  files.push(f);
});

app.get('/resonate/bts/editor', function(req, res) {
  res.render('editor', {
    files: files
  });
});

app.use(require('compression')());
app.use(express.static(__dirname));
app.listen(8080);

Any ideas would be fantastic, thank you!

@ericf
Copy link
Owner

ericf commented Apr 17, 2015

Makes sense that you'd get a type error when setting views to an array based on the current implementation of renderView(). This is also related to #112.

To support views being an array this code would need to be refactored. A new private method could be added to resolve view name, it could iterate over the views array until it finds a path which is contained in the viewPath string (indexOf === 0).

@sundarj
Copy link
Author

sundarj commented Apr 20, 2015

Ah right, I see. Thanks a lot!

@sundarj sundarj closed this as completed Apr 20, 2015
@sundarj sundarj linked a pull request Apr 20, 2015 that will close this issue
@sundarj sundarj reopened this Apr 20, 2015
@vanraizen
Copy link

I just ran into this issue today as well. I imagine this will come up more as an issue as people migrate to Express4 / use multiple view directories.

@woshi82
Copy link

woshi82 commented Sep 8, 2016

did you fix the problem?it still throw err 'Path must be a string',when i set the views to an array .
-_-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants