错误:无法读取null的属性(阅读'name')当使用ejs与nodejs

ttygqcqt  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(209)

这个错误发生在我使用ejs的时候,否则它在fetch请求或postman请求中工作正常。它只发生在品牌数据上,当我使用ejs时,它在第一个请求中发出双重请求,它向客户端发送数据,但在第二个请求中它发送null,这就是错误发生的原因,但不知道为什么它发送null并发出双重请求。

后台

const brandId = req.params.id;
  const brand = await Brand.findOne({ _id: brandId });
  const vehicles = await Vehicle.find({ brand: brandId });
  console.log(brand);
  res.render("brand", { brand, vehicles });

前端

<!DOCTYPE html>
<html lang="en">
  <head>
    <%- include('partials/_head'); %>
    <title>Home</title>
  </head>
  <body>
    <%- include('partials/_header'); %>
    <div class="container">
      <main>
        <section class="showcase">
          <div class="showcase-brand">
            <div class="brand-title"><%= brand.name %> vehicles</div>
            <div class="brand-logo">
              <img src="<%= brand.image %>" alt="brand-logo" />
            </div>
          </div>
        </section>
        <section class="brand-vehicles">
          <div class="brand-vehicles-list">
            <% vehicles.forEach(vehicle => { %>
            <a href="#" class="brand-vehicle">
              <img
                src="https://s7d1.scene7.com/is/image/hyundai/2023-sonata-hybrid-limited-ultimate-red-019:Vehicle-Carousel?fmt=webp-alpha"
                alt="vehicle-img"
              />
              <p><%= vehicle.name %></p>
            </a>
            <% }) %>
          </div>
        </section>
      </main>
      <%- include('partials/_sidebar'); %>
    </div>
  </body>
</html>

控制台日志

Server is running on port:3000...
`{
  _id: 'tesla',
  name: 'tesla',
  image: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Tesla_Motors.svg/800px-Tesla_Motors.svg.png',
  createdAt: 2023-05-23T08:21:27.743Z,
  updatedAt: 2023-05-23T08:21:27.743Z,
  __v: 0
}
null
TypeError: C:\Users\winne\Documents\wikiwheels\views\brand.ejs:13
    11|         <section class="showcase">
    12|           <div class="showcase-brand">
 >> 13|             <div class="brand-title"><%= brand.name %> vehicles</div>
    14|             <div class="brand-logo">
    15|               <img src="<%= brand.image %>" alt="brand-logo" />
    16|             </div>

Cannot read properties of null (reading 'name')
    at eval ("C:\\Users\\winne\\Documents\\wikiwheels\\views\\brand.ejs":18:32)
    at brand (C:\Users\winne\Documents\wikiwheels\node_modules\ejs\lib\ejs.js:703:17)
    at tryHandleCache (C:\Users\winne\Documents\wikiwheels\node_modules\ejs\lib\ejs.js:274:36)
    at exports.renderFile [as engine] (C:\Users\winne\Documents\wikiwheels\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\application.js:657:10)
    at Function.render (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\application.js:609:3)
    at ServerResponse.render (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\response.js:1039:7)
    at getBrand (C:\Users\winne\Documents\wikiwheels\controller\brand.js:23:7)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  path: 'C:\\Users\\winne\\Documents\\wikiwheels\\views\\brand.ejs'
}
null
TypeError: C:\Users\winne\Documents\wikiwheels\views\brand.ejs:13
    11|         <section class="showcase">
    12|           <div class="showcase-brand">
 >> 13|             <div class="brand-title"><%= brand.name %> vehicles</div>
    14|             <div class="brand-logo">
    15|               <img src="<%= brand.image %>" alt="brand-logo" />
    16|             </div>

Cannot read properties of null (reading 'name')
    at eval ("C:\\Users\\winne\\Documents\\wikiwheels\\views\\brand.ejs":18:32)
    at brand (C:\Users\winne\Documents\wikiwheels\node_modules\ejs\lib\ejs.js:703:17)
    at tryHandleCache (C:\Users\winne\Documents\wikiwheels\node_modules\ejs\lib\ejs.js:274:36)
    at exports.renderFile [as engine] (C:\Users\winne\Documents\wikiwheels\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\application.js:657:10)
    at Function.render (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\application.js:609:3)
    at ServerResponse.render (C:\Users\winne\Documents\wikiwheels\node_modules\express\lib\response.js:1039:7)
    at getBrand (C:\Users\winne\Documents\wikiwheels\controller\brand.js:23:7)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  path: 'C:\\Users\\winne\\Documents\\wikiwheels\\views\\brand.ejs'
}`
2jcobegt

2jcobegt1#

错误通常发生在我们在单个入口点调用多个路由时,如果你不相信,那么只是为了测试注解其他路由,那么相同的路由将正常工作。如果你使用的是restAPI,那么一定要仔细检查所有的路由是否在你用同一个入口点调用多个路由的地方。

相关问题