由于:TypeError:handlebars is not a function,无法设置车把
Posted
技术标签:
【中文标题】由于:TypeError:handlebars is not a function,无法设置车把【英文标题】:Unable to set up handlebars due to : TypeError: handlebars is not a function 【发布时间】:2022-01-01 22:40:30 【问题描述】:const express = require('express');
const app = express();
//set up handlebars
const handlebars = require('express-handlebars');
app.engine("handlebars",handlebars())
app.set("view engine", "handlebars")
const sqlite3 = require('sqlite3');
const sqlite = require('sqlite');
const port = 8080
const dbPromise = sqlite.open(
filename:"./database/sakila.sqlite",
driver: sqlite3.Database
)
app.get("/",(req,res)=>
res.render("home",layout:false);
)
app.listen(port,()=>
console.log(`server running on $port`);
)
我正在观看一个讲座视频来了解车把。我逐字输入相同的代码来设置车把,然后我执行了 npm install express-handlebars --save。讲师的代码有效,但我的无效。 它只是警告我
"app.engine("车把",车把()) ^
TypeError:车把不是函数
" 我不知道为什么。请帮忙。提前谢谢你。
【问题讨论】:
简单阅读the documentation,上面写着:import engine from 'express-handlebars'; app.engine('handlebars', engine())
。我假设你也可以使用app.engine("handlebars",handlebars.engine())
【参考方案1】:
const express = require("express");
const handlebars = require("express-handlebars");
const app = express();
//set up handlebars
app.engine("handlebars", handlebars());
app.set("view engine", "handlebars");
const sqlite3 = require("sqlite3");
const sqlite = require("sqlite");
const port = 8080;
const dbPromise = sqlite.open(
filename: "./database/sakila.sqlite",
driver: sqlite3.Database
);
app.get("/", (req, res) =>
res.render("home", layout: false );
);
app.listen(port, () =>
console.log(`server running on $port`);
);
【讨论】:
以上是关于由于:TypeError:handlebars is not a function,无法设置车把的主要内容,如果未能解决你的问题,请参考以下文章
js模版引擎handlebars.js实用教程——由于if功力不足引出的Helper
如何通过 Handlebars.precompile (vs CLI) 使用模板