首页 > 解决方案 > 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)

标签: javascriptnode.jsexpresspassport.jsejs

解决方案


您需要确保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>'
  })
);

推荐阅读