Node.js 为什么get route显示TypeError:无法读取属性';标题';对空值和网站崩溃的影响?

Node.js 为什么get route显示TypeError:无法读取属性';标题';对空值和网站崩溃的影响?,node.js,express,mongoose,typeerror,Node.js,Express,Mongoose,Typeerror,我正在nodejs中使用mongodb构建一个express应用程序,当我向show posts路由发出get请求时,当我在ejs文件中传递这些帖子时,所有帖子都会正确呈现,但在显示show页面后,终端会出现错误 events.js:291投掷者;//未处理的“错误”事件^TypeError:无法读取null的属性“title” 这是我在router/posts.js中的获取路径 router.get("/commerce", (req,res)=>{

我正在nodejs中使用mongodb构建一个express应用程序,当我向show posts路由发出get请求时,当我在ejs文件中传递这些帖子时,所有帖子都会正确呈现,但在显示show页面后,终端会出现错误

events.js:291投掷者;//未处理的“错误”事件^TypeError:无法读取null的属性“title”

这是我在router/posts.js中的获取路径

    router.get("/commerce", (req,res)=>{
  
  if(req.query.search){
    var noMatch;
// gives search results on author name, content and title of the post
    const regex = new RegExp(escapeRegex(req.query.search), 'gi') 
    Post.find({$or: [{title:regex} , {content:regex}, {'author.username':regex}], subject: "commerce"}, function(err,allposts){
      if(err) console.log(err)
      else{
        
        if(allposts.length<1){
          noMatch = "No posts matched the search results , please try again"
        }
        console.log("searched", allposts)
        res.render("commerce", {posts: allposts, noMatch: noMatch, message: req.flash('success')});
      }
    })
    
  } else{
    Post.find({subject: "commerce"}, function(err,allposts){
      if(err) {
        console.log(err);
        res.statusCode = 500;
        res.end('error');
      }
      else{
        console.log("actually all posts",allposts)
        res.render("commerce", {posts: allposts, noMatch: noMatch , message: req.flash('success')});
      }
    })
  }
})
router.get(“/commerce”,(req,res)=>{
if(请求查询搜索){
var noMatch;
//提供关于作者姓名、内容和文章标题的搜索结果
const regex=new RegExp(escapeRegex(req.query.search),'gi')
Post.find({$or:[{title:regex},{content:regex},{'author.username':regex}),主题:“commerce”},函数(err,allposts){
if(err)console.log(err)
否则{
if(allposts.length
趋向
具有集成数据处理功能的3D摄像机如何减少网络和主机PC上的负载
2020年8月15日
印地车的挡风玻璃时速超过200英里,安全性有所提高
2020年8月15日
危机期间的添加剂制造资格和认证
2020年8月15日
推荐
自动移动机器人和Cobot提高了工人的安全性和保留率
2020年8月15日
可持续控制面板设计
2020年8月15日
推入式端子与其他类型连接之间的区别
2020年8月15日
流行的
为什么在气动系统中使用IIoT既简单又关键
2020年8月15日
红外摄像机捕捉3D打印中的缺陷
2020年8月15日
危机期间的添加剂制造资格和认证
2020年8月15日
  • >
console.log()

-第1页,共1页-下一页 快速链接
  • a{ 文字装饰:无; 颜色:白色; } 社会的 发送 与我们联系 订阅通讯

    订阅我们的每两周一次的时事通讯
    以获得更多的知识曝光。
    此外,还有机会参加
    头脑风暴比赛
    并赢得激动人心的奖项

    订阅 隐私政策 版权所有&复制;保留所有权利|后座议员

    在这些输出中
    我在res.render()之前添加了console.log(“实际所有帖子”,allposts),并在打印所有帖子时获得了输出,最后还打印了null,该null来自何处?在该null之后,控制台上会打印错误,如上面的scrrenshots中所示

    将console.log(“实际所有帖子”,allposts)更改为console.log(“实际上是所有帖子”)。让我们看看终端中是否出现空值。是的,空值仍然存在。posts.js中的第197行是什么?第197行是console.log(““+post.title+”上的视图数是“+post.views”);在get route/commerce/:slug中,我在这里找到post wrt slug并呈现show.ejs,其中显示了这篇文章,在第194行,我做了console.log(post),它打印null(我猜)但是,如果我只是路由到/commerce,为什么会调用这个get路由呢?我解决了这个问题,这是一个相当愚蠢的错误,请求url应该是/commerce,但我请求了/commerce/这是另一个路由,并且在后一个请求之后/中期望有一个slug,它是空的,所以它显示为null post,因为没有pa就找不到post拉梅特。
    <%- include("./partials/header1.ejs") %>
    <link rel="stylesheet" href="/stylesheets/commerce.css">
    <%- include("./partials/header2.ejs") %>
    <div class="top-horiz-bar">
        <div class="logo-section-in-bar">
            <img class="top-img-logo" src="./finallogopic.png" alt="logo-pic" height="60px" width="60px">
            <!-- <img class="top-name-logo" src="./zoomed-brand-name.png" alt="logo-name" height="80px" width="130px"> -->
            <h2 class="top-name-logo">Backbanchers</h2>
        </div>
    
    
        <div class="other-icons">
    
        <div class="search-box">
            <input id="search-font" class="search-txt" type="text" name="" placeholder="Type to search">
            <a class="search-btn" href="#">
                <i class="fas fa-search"></i>
            </a>
        </div>
        <div class="person-account">
            <a class="account-info" href="#">
                <i class="far fa-user-circle fa-2x"></i>
                <span>Sign-in/up</span>
            </a>
        </div>
        </div>
    </div>
    
    
    <div class="vertical-nav-bar">
    <a href="#">Home<span><i class="fas fa-home"></i></span></a>
    <a class="blog-section-left" href="#">Blogs<span><i class="fas fa-user-graduate"></i></span>
    <!-- <ul class="sections-blog">
        <a  class="option-blog" href="#"><li>Business & Economics</li></a>
        <a  class="option-blog" href="#"><li>Commerce</li></a>
        <a  class="option-blog" href="#"><li>Personality Devlopment</li></a>
    </ul> -->
    </a>
    <a href="#">Authors<span><i class="fas fa-pencil-alt"></i></span></a>
    <a href="#">Newsletter<span><i class="fas fa-newspaper"></i></span></a>
    <a href="#">Contact US<span><i class="fas fa-phone-alt"></i></span></a>
    <!-- <a href="#">Services<span><i class="fas fa-question"></i></span></a> -->
    <a href="#">About Us<span><i class="fas fa-users"></i></span></a>
    <!-- <a href="#">Services<span><i class="fas fa-question"></i></span></a> -->
    
    
    </div>
    
    <div class="all-engineering-articles">
    
        <div class="side-box">
            <div class="one-of-3-heading">
                <h2 class="side-box-heading-one">Trending</h2>
                <div class="sub-part-section">
                <h2 class="title-side-box">How 3D cameras with integrated data processing reduce the load on network and host PC</h2>
                <div class="date-side-box">
                    <span>15 August 2020</span>
                </div>
            </div>
    
            <div class="sub-part-section">
                <h2 class="title-side-box">Indy Cars Get a Little Safer—Thanks to a 200+ mph Windshield</h2>
                <div class="date-side-box">
                    <span>15 August 2020</span>
                </div>
            </div>
    
            <div class="sub-part-section">
                <h2 class="title-side-box">Additive Manufacturing Qualification & Certification During Crises</h2>
                <div class="date-side-box">
                    <span>15 August 2020</span>
                </div>
            </div>
            </div>
    
    
            <div class="two-of-3-heading">
                <h2 class="side-box-heading-two">Recommended</h2>
                <div class="sub-part-section">
                    <h2 class="title-side-box">Autonomous Mobile Robots and Cobots Improve Worker Safety and Retention</h2>
                    <div class="date-side-box">
                        <span>15 August 2020</span>
                    </div>
                </div>
    
                <div class="sub-part-section">
                    <h2 class="title-side-box">Sustainable Control Panel Design</h2>
                    <div class="date-side-box">
                        <span>15 August 2020</span>
                    </div>
                </div>
    
                <div class="sub-part-section">
                    <h2 class="title-side-box">The Difference Between: Push-In Terminals versus Other Types of Connections</h2>
                    <div class="date-side-box">
                        <span>15 August 2020</span>
                    </div>
                </div>
            </div>
    
    
            <div class="three-of-3-heading">
                <h2 class="side-box-heading-three">Popular</h2>
    
                <div class="sub-part-section">
                    <h2 class="title-side-box">
                        Why Using IIoT for Pneumatics is Simple, Yet Critical</h2>
                    <div class="date-side-box">
                        <span>15 August 2020</span>
                    </div>
                </div>
    
                <div class="sub-part-section">
                    <h2 class="title-side-box">
                        IR Camera Captures Defects in 3D Printing</h2>
                    <div class="date-side-box">
                        <span>15 August 2020</span>
                    </div>
                </div>
    
                <div class="sub-part-section">
                    <h2 class="title-side-box">Additive Manufacturing Qualification & Certification During Crises</h2>
                    <div class="date-side-box">
                        <span>15 August 2020</span>
                    </div>
                </div>
            </div>
        </div>
    
    <% posts.forEach(function(post){ %>
        <div class="blog-post">
            <div class="blog-post__img">
                <img src="https://image.freepik.com/free-photo/cyborg-hand-pressing-keyboard-laptop-3d-rendering_117023-946.jpg" alt="article-pic" class="blog-post__article-img">
            </div>
            <div class="blog-post__info">
                <div class="blog-post__date">
                    <span><%= post.publishDay%></span>
                    <span><%= post.publish_date %></span>
                </div>
                <div class="author-name">
                    <ul class="author-content">
                        <li class="name-aut"><%= post.author.username %>></li>
                        <li class="save-to-later"><i class="fas fa-plus "></i></li>
                    </ul>
                </div>
                console.log(<%= post.title %>)
                <h1 class="blog-post__title"><%= post.title %></h1>
                <p class="blog-post__text"><%= post.content.substring(0,120) %></p>
                <a href="/posts/commerce/<%= post.slug %>" class="blog-post__cta">Read more</a>
                <ul class="btns-on-blogcard">
                    <!-- <li class="btns-blog"><i class="far fa-eye fa-2x "></i></li> -->
                    <li class="btns-blog"><i class="far fa-hand-peace fa-2x "></i></li>
                    <li class="btns-blog"><i class="fas fa-share fa-2x "></i></li>
                </ul>
            </div>
        </div>
    <% }) %>
    
    
    
    <div class="page-end">
        &nbsp;
    </div>
    
    <div class="next-page">
        <h2 class="new-page-blogs">-Page 1 of 1- <span class="next-page-btn">Next page</span></h2>
    </div>
    
    
    
    </div>
    
    
    <section class="footer" >
    <footer id="foo">
        <div class="overall-footer">
            <div class="first-part">
                <h2 class="our-name"><a href="#">Backbenchers</a></h2>
                <a href="#"><img id="logo-of-learners" src="finallogopic.png" alt="our-logo" height="40%" width="14%"></a>
            </div>
            <div class="bordering-right">
                &nbsp;
            </div>
    
    
    
            <div class="second-part">
                <h2 class="second-links">Quick Links</h2>
                <ul class="ul-class-footer">
                    <li class="quick-buttons"><a href="#">Home</a></li>
                    <li class="quick-buttons"><a href="#Buisness-and-economics-start">Buisness & Economics</a></li>
                    <li class="quick-buttons"><a href="#Commerce">Commerce</a></li>
                    <li class="quick-buttons"><a href="#engineering">Engineering</a></li>
                    <li class="quick-buttons"><a href="#personality-D">Personality Devlopment</a></li>
                    
                </ul> 
            </div>
    
    
    
            
    
            <div class="third-party">
                
            <div class="name-social-footer">
            <li class="quick-buttons-2"><a href="https://www.facebook.com/" target="_blank">Facebook</a></li>
            <li class="quick-buttons-2"><a href="https://www.instagram.com/" target="_blank">Instagram</a></li>
            <li class="quick-buttons-2"><a href="https://www.linkedin.com/" target="_blank">LinkedIn</a></li>
        </div>
        <style>
            a{
                text-decoration: none;
                color: white;
            }
        </style>
                 <div class="media-buttons-footer">
                    <a class="btns-footer-class-1" href="#"><i id="facebook-1" class="fab fa-facebook-f "></i></a>
                    <a class="btns-footer-class-2" href="#"><i id="instagram-1" class="fab fa-instagram "></i></a>
                    <a class="btns-footer-class-3" href="#"><i id="linkedin-1" class="fab fa-linkedin-in "></i></a>
                </div> 
            </div>
    
    
    
    
    
            <div class="fourth-party">
                <h2 class="fourth-links">Social</h2>
                <a href="mailto:ritishgupta45@gmail.com">
                <span id="envolope-footer" class="fas fa-envelope fa-2x"></span>
                <span id="email-footer" class="text">Backbenchers@gmail.com</span>
                </a>
                
              
    
            <input id="name-id-id" type="email" placeholder="Name">
            <input id="email-id-id" type="email" placeholder="Email-id" required>
            <input id="leave-msg-id-id" type="text" placeholder="leave a message">
            <div class="send-button-footer">
                <button id="send-id-id" type="submit">Send</button>  
              </div>
            
            </div>
    
    
    
    
            
            <div class="fifth-party">
                <h2 class="fifth-links">Connect with Us</h2>
                <h2 class="heading-of-news-footer">Newsletter subscription</h2>
                <p class="para-of-news-footer">Subscribe to our fortnightly newsletter <br> to gain great knowledge exposure. <br> Also, stand a chance to participate <br> in brainstorming competitions <br> and win exciting prizes.</p>
                <div class="suscribe-footer">
                    <input id="footer-suscribe-btns" type="email" placeholder="enter your email id" required>
                    <div class="red-btn-suscribe">
                    <button id="suscribing" type="button">Subscribe</button>
                </div>
            </div>
        </div>
        
    
    
    
    
        <div class="final-part">
            <div class="box-copy">
                &nbsp;
                <div class="btns-margin">
               <a href=""><h2 class="bottom-copy-footer">About Us</h2></a> 
                <a href=""></a><h2 class="bottom-copy-footer">Privacy Policy</h2></a>
               <a href=""> <h2 class="bottom-copy-footer">Terms and conditions</h2></a>
            </div>
    
            <h2 class="final-copywrite">copyright &copy; All Rights Reserved | Backbenchers</h2>
            </div>
        </div>
    
    </footer> 
    </section>
    
    
    
    
    
    <%- include("./partials/footer.ejs") %>