无法读取undefined的属性'title'.表达

 孩子气zyj2 发布于 2023-02-12 15:59

嗨我正在开发nodejs表达应用程序.我越来越异常,不知道为什么.一切似乎都适合我.我的例外如下所示:

 500 TypeError: C:\Nodejs\NodejsBlog\apps\blog/views/postdetail.jade:23
21| .col- md-12
22| .posts
> 23| h3= post.title
24| p=post.body
25| p tag:
26| i=post.tag

Cannot read property 'title' of undefined 21| .col-md-12 22| .posts > 23| h3= post.title 24| p=post.body 25| p tag: 26| i=post.tag Cannot read property 'title' of undefined at eval (eval at (C:\Nodejs\NodejsBlog\node_modules\jade\lib\jade.js:152:8), :221:59) at C:\Nodejs\NodejsBlog\node_modules\jade\lib\jade.js:153:35 at Object.exports.render (C:\Nodejs\NodejsBlog\node_modules\jade\lib\jade.js:197:10) at Object.exports.renderFile (C:\Nodejs\NodejsBlog\node_modules\jade\lib\jade.js:233:18) at View.exports.renderFile [as engine] (C:\Nodejs\NodejsBlog\node_modules\jade\lib\jade.js:218:21) at View.render (C:\Nodejs\NodejsBlog\node_modules\express\lib\view.js:76:8) at Function.app.render (C:\Nodejs\NodejsBlog\node_modules\express\lib\application.js:504:10) at ServerResponse.res.render (C:\Nodejs\NodejsBlog\node_modules\express\lib\response.js:798:7) at C:\Nodejs\NodejsBlog\apps\blog\routes.js:64:14 at callbacks (C:\Nodejs\NodejsBlog\node_modules\express\lib\router\index.js:164:37)

这是app.post代码:

app.get('/Post/:id',function(req,res){
    var postdata;
    var comments;
    Post.findOne({_id:req.params.id},function(err, docs){
            if(docs) {
                postdata=docs;  
                console.log('Gönderi bulundu');
                console.log(docs);
                console.log(postdata);
                console.log(postdata.title);
            } else {

                console.log('Gönderi bulunamad?');
            }
        });

        Comment.findOne({postid:req.params.id},function(err, docs){
            if(docs) {
                console.log('Yorum bulundu');
                console.log(docs);
            } else {
                comments=docs;  
                console.log('Yorum bulunamad?');
            }
        });

    return res.render(__dirname+"/views/postdetail",{
            title: 'adfasdf',
            stylesheet: 'postdetail',
            post:postdata,
            comments:comments
            });
});

我的观点是:

extends ../../../views/bloglayout
block js
script(type='text/javascript')
    $(function() {
        $("#commentform" ).submit(function( event ) {
            alert( "Handler for .submit() called." );
            $.ajax({
                url: '/Post/Comment/',
                type: "POST",
                data: $('#commentform').serialize(),
                success: function(response){
                alert('Yorum Kaydedildi');
                }
            }); 
            event.preventDefault();
        });
    });

block content
.row
    .col-md-12
        .posts
            h3=post.title
            p=post.body
            p tag:
                i=post.tag
            p Anahtar Kelimeler:
                b=post.keywords
        .row
            .col-md-4
                h5 Yorum Yap
                  form#commentform(role='form',action='/Post/Comment', method='post')
                            input(type='hidden',name='comment[postid]',value=postdata._id)
                        .form-group
                            input.form-control(type='email',name='comment[email]',placeholder='E-posta adresi')
                        .form-group
                            input.form-control(type='text',name='comment[website]', placeholder='Website')
                        .form-group
                            textarea.form- control(type='text',name='comment[content]', placeholder='Yorum')
                        button.btn.btn-  default(type='submit') Ekle
                -comments.forEach(function(comment) {
                .well
                    p
                        b=comment.content
                    p=comment.email
                -})

我也检查了我的mongodb.有数据.我不知道为什么'title'属性'undefined'不知道.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有