javascript - ReferenceError node.js, ejs express bcrypt 护照
问题描述
最近开始学习node.js,我正在尝试组合来自不同项目的代码,一切正常,直到我将路由路径从'/dashboard'更改为'/store'以及index.js中的相应回调函数。
store.ejs 文件添加 'p' 元素以显示用户名
store.ejs(视图)
<!DOCTYPE html>
<html>
<head>
<title>The Generics | Store</title>
<meta name="description" content="This is the description">
<link rel="stylesheet" href="styles.css" />
<link rel="stylesheet" href="../css/slideshow.css" />
<script src="https://checkout.stripe.com/checkout.js" defer></script>
<script>
var stripePublicKey = '<%= stripePublicKey %>'
</script>
<script src="store.js" defer></script>
</head>
<body>
<!-- this is added code-->
<h1 class="mt-4">Dashboard</h1>
<p class="lead mb-3">Welcome <%= user.name %></p>
<a href="/users/logout" class="btn btn-secondary">Logout</a>
</body>
</html>
index.js(路由)
const express = require('express');
const router = express.Router();
const { ensureAuthenticated, forwardAuthenticated } = require('../config/auth');
// Welcome Page
router.get('/', forwardAuthenticated, (req, res) => res.render('welcome'));
// Dashboard, change the route path to store
router.get(/*'/dashboard'*/'/store', ensureAuthenticated, (req, res) =>
res.render(/*'dashboard'*/'store', {
user: req.user
})
);
module.exports = router;
错误文本
ReferenceError: C:\Users\lee hao zheng\Desktop\GroceryStore\views\store.ejs:10
8| <script src="https://checkout.stripe.com/checkout.js" defer></script>
9| <script>
>> 10| var stripePublicKey = '<%= stripePublicKey %>'
11| </script>
12| <script src="store.js" defer></script>
13| </head>
stripePublicKey is not defined
at eval (eval at compile (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\ejs\lib\ejs.js:661:12), <anonymous>:12:26)
at store (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\ejs\lib\ejs.js:691:17)
at tryHandleCache (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\response.js:1012:7)
at ServerResponse.res.render (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
at C:\Users\lee hao zheng\Desktop\GroceryStore\routes\index.js:10:7
at Layer.handle [as handle_request] (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\router\route.js:137:13)
at ensureAuthenticated (C:\Users\lee hao zheng\Desktop\GroceryStore\config\auth.js:4:14)
at Layer.handle [as handle_request] (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\lee hao zheng\Desktop\GroceryStore\node_modules\express\lib\router\route.js:112:3)
解决方案
您需要确保stripePublicKey
像这样传递给您的 EJS 模板:
// Dashboard, change the route path to store
router.get(/*'/dashboard'*/'/store', ensureAuthenticated, (req, res) =>
res.render(/*'dashboard'*/'store', {
user: req.user
// Here make sure to pass key to temlate
stripePublicKey: '<insert key here>'
})
);
推荐阅读
- python - 无法将 pandas 日期时间列与从数据库中获取的日期时间进行比较
- java - Spring Data JPA 查询方法在尝试通过 List 属性查找所有实体时抛出奇怪的错误消息
- animation - Blazor wasm 动画形状到坐标
- python - 收到的标签值 17 超出 [0, 12) 的有效范围 - Keras Python
- typescript - TypeScript - 包含其他泛型类型的类字典泛型 - 泛型类型在方法中丢失的问题
- php - 在同一台服务器上托管 Nodejs 和 PHP
- csv - 作业前测试文件名
- c++ - 无法根据对象的属性值对对象数组进行排序
- r - 如何在此情节中添加图例?
- python - 如何在熊猫中将“0天00:39:00”转换为只有39?