首页 > 解决方案 > 对象属性未定义

问题描述

通过递归,我以数据库查询结果的形式将数据添加到 obj['prop'] 属性。

let obj = {};

if (obj['prop'] == undefined) {
  obj['prop'] = {};
}

console.log(obj);

为什么这不起作用?我也试过这个。

if(!obj['prop']){
 obj['prop'] = {}
}

我知道这与呈现的错误无关......

但是,我将具有多个嵌套属性的值应用于对象。

obj['prop']['prop2'] = results

它不允许我这样做,所以我在添加更多嵌套属性之前应用了对象内部的属性。但它不起作用。它仍在提出未定义的属性“prop”。

我的功能:

 posts: function(request, cb){
            application = (request.query.application) ? request.query.application : "*";
            type = (request.query.type) ? request.query.type : "*";
            wall = (request.query.wall) ? request.query.wall : "*";
            db.query(
                "SELECT * FROM Client.Posts WHERE Application = ? AND Wall_Type = ? AND Wall_Name = ?;",
                [
                    application,
                    type,
                    wall
                ],
                function(error, posts){
                    if(posts.length == 0){
                        cb(posts);
                    }else{
                        repliesDone = false;
                        likesDone = false;
                        commentsDone = false;
                        commentLikesDone = false;
                        repliesLikesDone = false;
                        function PostRecursionComments(posts, length, i){
                            if(posts[i]['DATA'] == undefined){
                                posts[i]['DATA'] = {};
                            }
                            db.query(
                                "SELECT * FROM Client.Comments WHERE Client.Comments.Post_ID = ?",
                                [
                                    posts[i]['ID']
                                ],
                                function(error, comments){
                                    for(i = 0; i < comments.length; i++){
                                        db.query(
                                            "SELECT * FROM Client.Likes WHERE Type = 'Comment' AND TypeID = ?",
                                            [
                                                comments[i]['ID']
                                            ],
                                            function(error, commentLikes){
                                                commentLikesDone = true;
                                                posts[i]['DATA']['Comments'][i]['DATA']['Likes'] = commentLikes;
                                            }
                                        );
                                    }
                                    posts[i]['DATA']['Comments'] = comments;
                                    if(posts.length - 1 == i){
                                        commentsDone = true;
                                    }else{
                                        i++;
                                        PostRecursionComments(posts, posts.length, i);
                                    }
                                }
                            );
                        }
                        PostRecursionComments(posts, posts.length, 0);
                        function CommentsRecursionReplies(comments, length, i, z, posts){
                            if(comments[z]['DATA'] == undefined){
                                comments[z]['DATA'] = {};
                            }
                            db.query(
                                "SELECT * FROM Client.Replies WHERE Client.Replies.Parent_Is_Comment = 1 AND Client.Replies.Parent_ID = ?",
                                [
                                    commentsz[z]['ID']
                                ],
                                function(error, replies){                                       
                                    for(i = 0; i < replies.length; i++){
                                        db.query(
                                            "SELECT * FROM Client.Likes WHERE Type = 'Reply' AND TypeID = ?",
                                            [
                                                replies[i]['ID']
                                            ],
                                            function(error, replyLikes){
                                                replyLikesDone = true;
                                                comments[z]['DATA']['Replies'][i]['DATA']['Likes'] = replyLikes;
                                            }
                                        );
                                    }
                                    comments[z]['DATA']['Replies'] = replies;
                                    if(posts[i]['DATA']['Comments'].length - 1 == z){
                                    }else{
                                        z++;
                                        CommentsRecursionReplies(posts[i]['DATA']['Comments'], posts['DATA']['Comments'].length, i, z, posts);
                                    }
                                }
                            );
                        }
                        for(i = 0; i < posts.length; i++){
                            if(posts[i]['DATA']['Comments']){
                                CommentsRecursionReplies(posts[i]['DATA']['Comments'], posts[i]['DATA']['Comments'].length, i, 0, posts);
                            }
                            repliesDone = true;
                        }
                        function RepliesRecursionReplies(replies, length, i, j, z, posts){
                            if(replies[z]['DATA'] == undefined){
                                replies[z]['DATA'] = {};
                            }
                            /*  truth tables? */
                            db.query(
                                "SELECT * FROM Client.Replies WHERE Client.Replies.Parent_Is_Comment = 0 AND Client.Replies.Parent_ID = ?",
                                [
                                    posts[i]['DATA']['Comments'][j]['Replies'][z]['ID']
                                ],
                                function(error, replies){
                                    if(!posts[i]['DATA']['Comments'][j]['Replies'][z]['DATA']){
                                        posts[i]['DATA']['Comments'][j]['Replies'][z]['DATA'] = {};
                                    }
                                    posts[i]['DATA']['Comments'][j]['Replies'][z]['DATA']['Replies'] = replies;
                                    if(posts[i]['DATA']['Comments'][j]['Replies'].length - z == 0){
                                        /* enumerator and properties... */
                                        repliesRepliesDone = true;
                                    }else{
                                        z++;
                                        RepliesRecursionReplies(replies, replies.length, i, j, z, posts);
                                    }
                                }
                            );
                        }
                        for(i = 0; i < posts[i].length; i++){
                            for(j = 0; j < posts[i]['DATA']['Comments'].length; j++){
                                RepliesRecursionReplies(posts[i]['DATA']['Comments'][j]['Replies'], posts[i]['DATA']['Comments'][j]['Replies'].length, i, j, 0, posts);
                            }
                        }
                        function PostRecursionLikes(posts, length, i){
                            if(posts[i]['DATA'] == undefined){
                                posts[i]['DATA'] = {};
                            }
                            db.query(
                                "SELECT * FROM Client.Likes WHERE Type = 'Post' AND TypeID = ?",
                                [
                                    posts[i]['ID']
                                ],
                                function(error, likes){
                                    post['DATA']['Likes'] = likes;
                                    if(posts.length - 1 == i){
                                        likesDone = true;
                                    }else{
                                        i++;
                                        PostRecursionLikes(posts, posts.length, i);
                                    }
                                }
                            );
                        }
                        PostRecursionLikes(posts, posts.length, 0);
                        function callback(data){
                            if(commentsDone && repliesDone && likesDone && commentLikesDone && replikesLikesDone){
                                cb(data);
                            }else{
                                setTimeout(function(){
                                    callback(data)
                                }, 1);
                            }
                        }
                        callback(posts);
                    }
                }
            );
        }

标签: javascriptobjectpropertiesundefined

解决方案


它确实不起作用。

那是因为它不是真的未定义它是空的。这意味着它可以是' 'null。因此,也尝试解决这些条件,它应该可以完美运行!

let obj = {};

if(obj['prop'] == 'undefined' || obj['prop'] == '' || obj['prop'] == null){
  obj['prop'] = {};
}
console.log(obj['prop'])


推荐阅读