Посредством рекурсии я добавляю данные в свойство 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);
}
}
);
}