【问题标题】:Cannot read property 'name' of null node无法读取空节点的属性“名称”
【发布时间】:2020-02-22 19:01:43
【问题描述】:

架构

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});
        }
    });
});

show.ejs

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

<%- include('partials/footer')  %> 

错误

TypeError: /home/ec2-user/environment/Yelpcamp/v2/views/show.ejs:3`
    1| <%- include('partials/header')  %> `
    2| <h1>this is a show template</h1>`
 >> 3| <p><%= campground.name %></p>`
    4| <img src="<%=campground.image%>">`
    5| 
    6| <%- include('partials/footer')  %>` 

无法读取 null 的属性“名称” 在 eval (/home/ec2-user/environment/Yelpcamp/v2/views/show.ejs:13:37) 在展会上(/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:656:17) 在 tryHandleCache (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:254:36) 在 View.exports.renderFile [作为引擎] (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:459:10) 在 View.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/view.js:135:8) 在 tryRender (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/application.js:640:10) 在 Function.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/application.js:592:3) 在 ServerResponse.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/response.js:1012:7) 在 /home/ec2-user/environment/Yelpcamp/v2/app.js:74:17 在/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/model.js:4828:16 在/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:4390:12 在 model.Query.Query._completeOne (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:2074:12) 在 Immediate.Query.base.findOne.call (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:2136:10) 在立即。 (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mquery/lib/utils.js:116:16) 在 runCallback (timers.js:705:18) 在 tryOnImmediate (timers.js:676:5)

即使在重新启动 mongodb 后我也会收到此错误 以及如何检查名称是否为空

【问题讨论】:

  • 你确定你得到的是来自猫鼬的结果吗?似乎 foundCampgrounds 为空。你能console.log吗?
  • 是的,它显示 foundCampgrounds 为空,但我不明白为什么
  • 做 req.params.id ,而不是_id
  • Lol yelpcamps..它来自一些 udemy 课程吗?我记得
  • 谢谢你,是的,它来自 udemy

标签: javascript node.js express ejs aws-cloud9


【解决方案1】:

它必须是不带下划线的 req.params.id :)

【讨论】:

  • 很好的答案,但请提供更专业的答案。使用示例和文档资源来强化您的观点。特别是关于 request 和 params 主题的更多信息,并带有不同的示例。
  • 请在答案中添加一些解释
【解决方案2】:

解决办法是:

  1. 重启服务器后,打开“/campgrounds”页面,我这里是“http://localhost:3000/campgrounds”
  2. 之后点击更多信息。

我也遇到了同样的问题,但是当我从 /campgrounds 页面打开时,它就解决了

【讨论】:

    【解决方案3】:

    问题的解决方法有点简单,就在这行代码

    res.redirect("/campgrounds"{campground:newCampgrounds})

    您可能缺少将 newCampgrounds 引用到将在 show.ejs 中使用为 campground.namecampground.image 等的露营地..

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 2018-04-16
      • 2013-03-15
      • 2020-10-24
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      相关资源
      最近更新 更多