【问题标题】:ERR_INVALID_ARG_TYPE - The "from" argument must be of type string. Received an instance of ArrayERR_INVALID_ARG_TYPE - “from”参数必须是字符串类型。收到 Array 的实例
【发布时间】:2020-08-30 13:30:37
【问题描述】:

我正在尝试为视图文件夹添加两个目录。在文档中,我发现您可以添加字符串或数组以添加多个视图文件夹。

一个用于管理面板,另一个用于网站/公共使用。

所以我按如下方式添加:

/**
 * setting up the view engine
 */
app.engine('handlebars', hbs({
    defaultLayout: "main"
}));
app.set('view engine', 'handlebars');

app.set('views', [path.join(__dirname, 'views'), path.join(__dirname, 'public-views/zubizi-theme')]);
// end view engine setup

我收到以下错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received an instance of Array

(node:108) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received an instance of Array
    at validateString (internal/validators.js:117:11)
    at Object.relative (path.js:437:5)
    at ExpressHandlebars.renderView (F:\zubizi\cms\files\node_modules\express-handlebars\lib\express-handlebars.js:193:38)
    at View.render (F:\zubizi\cms\files\node_modules\express\lib\view.js:135:8)
    at tryRender (F:\zubizi\cms\files\node_modules\express\lib\application.js:640:10)
    at Function.render (F:\zubizi\cms\files\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (F:\zubizi\cms\files\node_modules\express\lib\response.js:1008:7)
    at F:\zubizi\cms\files\routes\admin.js:7:6
    at Layer.handle [as handle_request] (F:\zubizi\cms\files\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\zubizi\cms\files\node_modules\express\lib\router\route.js:137:13)
(node:108) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:108) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

.

这是我的文件夹结构如下:

.

我在我的应用中使用了以下技术:

NodeJS 模板引擎express-handlebars

ExpressJS 版本:4.16.0

Node JS版本:v12.16.3

【问题讨论】:

    标签: javascript node.js express handlebars.js express-handlebars


    【解决方案1】:

    有一个开放的 PR 支持作为数组传入的多个视图目录:https://github.com/ericf/express-handlebars/pull/209

    现在,您需要修复您的设置并将视图合并到一个目录中并仅传递该目录:

    app.set('views', './path-to-views-folder');

    【讨论】:

    • 还有其他方法可以使用两个视图文件夹吗?还是我需要从 express-handlebars 转移到任何其他模板引擎?
    • 根据 PR 不幸的是,没有 express-handlebars,在我自己的项目中,我使用了支持它的 ejs(请参阅 stackoverflow.com/a/11326059/3761628
    • @HelloWorld:如果这回答了您的问题,请通过接受答案来关闭它。谢谢你:)
    猜你喜欢
    • 2020-11-21
    • 2021-12-24
    • 2022-11-10
    • 1970-01-01
    • 2021-07-15
    • 2019-11-10
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多