简体   繁体   English

当我尝试渲染 EJS 页面时,我不断收到类型错误...,有人可以看看吗?

[英]I keep getting a type error when I try to render an EJS page... ,can someone have a look?

Here is the index.js code这是 index.js 代码

    enter code here
    CODE
    const express = require('express');
    const app = express();
    const path = require('path');

    app.use(express.urlencoded({ extended: true }))enter code here // for parsing application/x- 
    www-form-urlencoded
    app.use(express.json()) // for parsing application/json
    app.set('views', path.join(__dirname, 'views'))
    app.set('view engine','ejs')

    const comments = [
        { 
            id:1,
            username: 'tom',
            comment: 'lol'
        },
        {
            id:2,
            username: 'joy',
            comment : 'lmao'
        },
        {
            id:3,
            username: 'ango',
            comment: 'lwkm'
        },
        {
            id:4,
            username: 'shaiyen',
            comment: 'leemao'
        }
    ]

    app.get('/comments',(req,res) => {
        res.render('comments/index',{comments})
    })
 
    app.get('/comments/new', (req,res) =>{
        res.render('comments/new')
    })

    app.post('/comments',(req,res) =>{
        const {username,comment} = req.body;
        comments.push({username,comment})
        res.redirect('/comments');
    })

    app.get('/comments/:id',(req,res) =>{
        const{id}= req.params;
        const comment = comments.find(c => c.id === parseInt(id));
        res.render('comments/show',{comment})
    })

    app.get('/tacos',(req,res) => {
        res.send("GET /tacos response")
    })

    app.post('/tacos', (req,res) =>{
        const  {meat, qty} = req.body;
        res.send(` here are your ${meat} and ${qty} tacos`)
    })

    app.listen(3000, () => {
        console.log("listening on port 3000")
    })

While here is the html file虽然这里是 html 文件

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Show</title>
</head>
<body>
    <h1>Your comment<%= comment.id %> </h1>
    <h2><%= comment.comment - comment.username %> </h2>
</body>
</html>

It keeps showing this error[TypeError: C:\Users\BARINE\Downloads\game\stuff\getpost\views\comments\show.ejs:10 8|它一直显示此错误[TypeError: C:\Users\BARINE\Downloads\game\stuff\getpost\views\comments\show.ejs:10 8|

9| <body>

10| 10|

<%= comment.id %> <%= 评论.id %>

11|     <h2><%= comment.comment - comment.username %> </h2>

12| </body>

13| </html>

Cannot read properties of undefined (reading 'id') at eval ("C:\Users\BARINE\Downloads\game\stuff\getpost\views\comments\show.ejs":12:34) at show (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\ejs\lib\ejs.js:703:17) at tryHandleCache (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\ejs\lib\ejs.js:274:36) at View.exports.renderFile [as engine] (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\ejs\lib\ejs.js:491:10) at View.render (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\view.js:135:8) at tryRender (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\application.js:657:10) at Function.render (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\application.js:609:3) at ServerResponse.render (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\response.js:1039:7) at C:\Users\BARINE\Downloads\game\stuff\getpost\index.js无法在显示 (C:\Users \BARINE\Downloads\game\stuff\getpost\node_modules\ejs\lib\ejs.js:703:17) 在 tryHandleCache (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\ejs\lib\ejs .js:274:36) 在 View.exports.renderFile [作为引擎] (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\ejs\lib\ejs.js:491:10) 在 View。在 tryRender (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules 渲染 (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\view.js:135:8) \express\lib\application.js:657:10) 在 Function.render (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\application.js:609:3) 在 ServerResponse。渲染 (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\response.js:1039:7) 在 C:\Users\BARINE\Downloads\game\stuff\getpost\index.js :50:9 at Layer.handle [as handle_request] (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\router\layer.js:95:5)] 1 :50:9 在 Layer.handle [as handle_request] (C:\Users\BARINE\Downloads\game\stuff\getpost\node_modules\express\lib\router\layer.js:95:5)] 1

Hey So I Just Ran Your Code And Everything Seems to be in order嘿,所以我刚刚运行了您的代码,一切似乎都井井有条

Now This Error Which You Got现在你得到的这个错误

Cannot read property 'id' of undefined
    at eval

Is Beacuse the parameter you passing as id don't exist in the comments array是因为您作为id传递的参数在comments数组中不存在

So when this route is called所以当这条路线被调用时

const comment = comments.find(c => c.id === parseInt(id)); //comment == undefined

and thou the error and Ejs compile throw an eval error你的错误和 Ejs 编译抛出一个 eval 错误

Since you new try using try and catch由于您新尝试使用 try and catch

so your code should be like所以你的代码应该像

app.get('/comments/:id',(req,res) =>{
   try{
    const{id}= req.params;
    const comment = comments.find(c => c.id === parseInt(id));
    if(comment){
      res.render('comments/show',{comment})
    }else{
      res.status(404).send("Resource Not Found")
     }
    }catch (err){
      // How you would like to handle your error 
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求不断获取错误代码:400-有人可以看看我在做什么错吗? - Request keeps on getting Error code: 400 - can someone please have a look on what I'm doing wrong? 当我尝试从数组中渲染对象时出现类型错误 - Type Error when i try to Render Objects out of an array 即使我的代码看起来很好,我仍然会收到“未捕获的TypeError:字符串不是函数”错误。任何人都可以看看并解释一下吗? - I keep getting a “Uncaught TypeError: string is not a function” error, even though my code seems to be fine. Can anyone have a look and explain? EJS - 我有一个请求查询对象的 res.render 错误 - EJS - I Have an error with the res.render of a request query object 当我尝试刷新页面时出现此错误 - When i try to refresh my page i have this error 当我尝试将接口分配给属性时出现类型错误 - getting type error when I try to assign interface to a property 简单登录系统出错。 我不断收到同样的错误有人可以帮助我吗? - Error in Simple login system. i keep getting this same error can someone help me? 当我尝试向jQuery中的div添加背景图片时,我不断收到404错误 - I keep getting a 404 error when I try to add a background image to a div in jQuery 我可以在EJS中渲染多个源 - Can I render multiple sources in EJS 当我尝试使用“.remove()”时出现错误 - Getting an error when I try to use ".remove()"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM