mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.74k stars 843 forks source link

Cannot read properties of null if CSS is linked #751

Closed tzwel closed 9 months ago

tzwel commented 9 months ago

I'm debugging this for over two hours already

const user = await prisma.User.findFirst({
    where: {
        publicId: req.params.userId
    }
})

console.log(user)

res.render('profile.ejs', {user})

This is the returned user object

  {
    id: 1,
    publicId: 'some id',
    username: '123',
    password: 'passwordhash'
  }

The user gets logged correctly (if the template is mostly empty), ejs renders the template correctly, but logs this error in the console: Cannot read properties of null (reading 'id')

Once I add CSS into the template <link rel="stylesheet" href="/card.css"> I get this error

 authorId: user.id
                                       ^

TypeError: Cannot read properties of null (reading 'id')

user.id becomes completely unusable everywhere and will just crash node

This is the weirdest bug i have ever encountered, I am not even sure if this is linked to ejs in any way. The bug seems to change its behavior in some random circumstances I can not pinpoint

tzwel commented 9 months ago

Looks like this is unrelated to ejs and happens with jade and mustache as well https://stackoverflow.com/questions/31329578/why-does-adding-a-css-link-to-my-jade-template-make-my-node-app-crash https://stackoverflow.com/questions/57242584/page-crashes-whenever-i-link-a-css-stylesheet-on-one-file-but-doesnt-on-anothe

I'll keep this issue open for a while because there's no clear solution for this afaik

tzwel commented 9 months ago
    <link rel="stylesheet" href="/card.css">

A single slash like this fixed the problem. Why, I have no idea