首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HBS时面临的问题

使用HBS时面临的问题
EN

Stack Overflow用户
提问于 2021-07-12 03:25:30
回答 1查看 286关注 0票数 0

我是一个完全的新手在后端,我正在学习速成和使用HBS作为模板引擎。每次在Express中使用hbs部分时,我都会遇到这种情况。

代码语言:javascript
复制
const express = require("express");
const app = express();
const hbs = require("hbs");
const path = require("path");
const port = process.env.PORT || 3000;

// public static path
const static_path = path.join(__dirname, "../public");
const templates_path = path.join(__dirname, "../templates/views");
const partials_path = path.join(__dirname, "../templates/partials");

app.set("view engine", "hbs");
app.set("views", templates_path);
hbs.registerPartial(partials_path);

app.use(express.static(static_path));

// routing
app.get("/", (req, res) => {
  res.render("index");
});

app.get("/about", (req, res) => {
  res.render("about");
});

app.get("/weather", (req, res) => {
  res.render("weather");
});

app.get("*", (req, res) => {
  res.status(404).render("404error");
});
app.listen(port, () => {
  console.log("listning on port " + port);
});

误差

代码语言:javascript
复制
throw new _exception2['default']('Attempting to register a partial called "' + name + '" as undefined');
        ^
Error: Attempting to register a partial called "D:\Projects\ExpressWeb\templates\partials" as undefined
    at HandlebarsEnvironment.registerPartial (D:\Projects\ExpressWeb\node_modules\handlebars\dist\cjs\handlebars\base.js:80:15)
    at Instance.registerPartial (D:\Projects\ExpressWeb\node_modules\hbs\lib\hbs.js:222:35)
    at Object.<anonymous> (D:\Projects\ExpressWeb\src\app.js:15:5)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  description: undefined,
  fileName: undefined,
  lineNumber: undefined,
  endLineNumber: undefined,
  number: undefined
}

增加这一点来增加诋毁,因为Stack溢出不允许我发布这个,因为我提升的代码比描述多,而且我不知道该问什么更多。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-12 07:33:15

根据文档,它是- hbs.registerPartials(partials_path);而不是hbs.registerPartial(partials_path);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68341577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档