[英]ReferenceError node.js, ejs express bcrypt passport
Start learning node.js recently, I am trying to combine code from different project, everything works fine until I change the route path from '/dashboard' to '/store' and the corresponding callback function in index.js.最近开始学习 node.js,我正在尝试组合来自不同项目的代码,一切正常,直到我将路由路径从 '/dashboard' 更改为 '/store' 以及 index.js 中相应的回调 function。
store.ejs file add the 'p' element to display the name of user store.ejs 文件添加 'p' 元素以显示用户名
store.ejs (views) 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 (routes) 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;
error text错误文本
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)
You need to make sure to pass stripePublicKey
to your EJS template like such:您需要确保将
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>'
})
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.