【问题标题】:Cannot read property 'name' of null error无法读取空错误的属性“名称”
【发布时间】:2020-10-24 06:18:27
【问题描述】:

我仍然是网络开发的婴儿。我收到以下错误,可以 有人帮我吗?

架构:

var campgroundSchema = new mongoose.Schema({
   name: String,
   image: String,
   description: String,
   _id: String
});
var Campground = mongoose.model("Campground", campgroundSchema);
app.get("/campgrounds/:id",function(req , res){
Campground.findById(req.params.id, function(err, foundCampgrounds){
                  if(err){
                          console.log(err);
                         }else{
                       res.render("show", {campground: foundCampgrounds});
                     }
                 });
            });

这是 index.ejs 模板:

    <div class="row text-center" style="display:flex; flex-wrap:wrap">
        <% campgrounds.forEach(function(campground){ %>
            <div class="col-md-3 col-sm-6"> 
                <div class="thumbnail">
                    <img src="<%= campground.image %>">
                        <div class="caption">
                             <h4> <%= campground.name %> </h4>
                         </div>
                         <p><a href="/campgrounds/ <%= campground._id %>" 
                           class="btn btn-primary">More Info</a>
                         </p>
                 </div>
             </div>
         <% }) %>
    </div>

show.ejs:

<%- include('partials/header')%> 
    <h1>this is a show template</h1>
    <p><%= campground.name %></p>
<%- include('partials/footer')%> 

类型错误:

 Cannot read property 'name' of null

【问题讨论】:

  • 你能在你res.render(..)页面的地方添加nodejs-code吗?
  • 不确定我是否理解您的问题?
  • 您必须检查 foundCampgrounds 是否存在。 Mongoose findById 如果找不到您的记录,请返回 undefined。您可以使用:if (foundCampgrounds) { ... }

标签: node.js express mongoose mongodb-query


【解决方案1】:

您应该检查是否已定义 campground - 您可以在 nodejs-controller 或模板中执行此操作。这是ejs 的示例:

<%- include('partials/header')%>
    <% if (campground) { %> 
      <h1>this is a show template</h1>
      <p><%= campground.name %></p>
    <% } else { %> 
      <h1>No camground data was found</h1> 
   <% } %>
<%- include('partials/footer')%> 

【讨论】:

  • 错误不再存在。我现在得到:没有找到露营地数据。你建议的其他部分。但这不可能是真的,因为我在数据库中有一个可用的露营地。还有什么建议吗?
  • 您确定在您的网址中有_id 设置为id-param 的记录吗?
  • 不确定我是否关注你....你能详细说明一下吗?
  • 嗯,你的 mongodb 记录有一个 _id 字段。由于您使用的是app.get("/campgrounds/:id"):id 将设置为req.params.id),所以问题是是否存在具有此 ID 的记录?如果不是,显然不会返回任何记录。
  • 你指的是这个吗? db.campgrounds.find() { "_id" : ObjectId("5efed7d4ac396d054f8b8693"), "name" : "Granite Hill", "image" : "images.pexels.com/photos/45241/tent-camp-night -star-45241.jpeg?auto=compress&cs=tinysrgb&h=350 ", "description" : "这是一座巨大的花岗岩山丘,没有浴室。没有水。美丽的花岗岩!", "__v" : 0 }
【解决方案2】:

在 index.ejs 上。使用 span 从 DB,如下

        <span> <%= campground._id %> </span>

为什么?因为 _id 是一个唯一的对象并且是自动生成的。甚至不需要 将其包含在 Schema 中,除非您想在哪种情况下自己创建它 然后,您将使用变量修改控制器。我为此苦苦挣扎,但现在 好的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多