【问题标题】:Database structure store comments and post数据库结构存储评论和帖子
【发布时间】:2018-03-15 18:13:14
【问题描述】:

我有两张桌子

posts_table

post_id |用户 ID |状态消息 |日期时间

cmets_table

cmets_id |用户 ID | post_id | cmets_message |日期时间

我的代码是这样的,我想显示每篇文章并在这篇文章中显示所有 cmets 消息

router.get('/', (req, res, next) => {


 connection.query('SELECT * FROM photos_status', (err, result) => {

   if(err){
   console.error(err);  
   }else{
    if(result.length >0){ 
        for(var i = 0; i < result . length ;i++){    

            var Temp = [];
         var post_id =result[i]. post_id;
         connection.query('SELECT * FROM comments WHERE post_id = ?', 
    [post_id], function (error, results) {
                    if (error) {
                        res.json({
                          status:false,
                          message:'there are some error with query'
                          })
                        }else{

                            res.status(200).json({

                                result ,
                                results
                            });  

                        }
                    })
        }

         }

   }
 });

});

我想从数据库中选择数据并像这样显示

   [ 
      { "post_id":"1",
        "user_id":"2",
         "status_x":"demo ..."

       " comments"[
                   { 
                 "user_id":"1",
                 "post_id":"1",
                  "comments_message":"demo..",
                    },
                 { 
                 "user_id":"2",
                 "post_id":"1",
                 "comments_message":"demo..",
                    }
               ]


       }

  ]

【问题讨论】:

  • 为您的问题添加完整的描述,并添加您尝试做的事情..
  • 它可以完整的描述添加所以请帮助我

标签: javascript node.js


【解决方案1】:

我想这会给你一些想法:

var allPosts = []
for(var i = 0; i < result.length ;i++){ 
    singlepost={}
    singlepost['post_id'] = result[i].post_id;
    singlepost['user_id'] = result[i].user_id ;
    singlepost['status_x'] = result[i].status_message;
    singlepost['comments'] = [];
    connection.query('SELECT * FROM comments WHERE post_id = ?', 
    [post_id], function (error, results) {
                    if (error) {
                        res.json({
                          status:false,
                          message:'there are some error with query'
                          })
                        }else{ res.status(200).json({
                                //loop over comments create an comment object
                                comment={}
                                //same as we did above for singlepost add values and push to array for each element
                                singlepost['comments'].push(comment)
                                result ,
                                results
                            });
                        }
                }
}

供参考: How to push JSON object in to array using javascript Dynamically Add Variable Name Value Pairs to JSON Object

【讨论】:

  • 在这种情况下,评论不推送输出是“allPosts”:[{“post_id”:1,“user_id”:1,“post_message”:“hi”,“cmets”:[]},但我 console.log(results);然后打印评论数据
【解决方案2】:
 if(result.length >0){ 



      var allPosts = []
       for(var i = 0; i < result.length ;i++){    
        var result1;
        singlepost={}
        singlepost['post_id'] = result[i].post_id;
        singlepost['user_id'] = result[i].user_id ;
        singlepost['post_message'] = result[i].post_messagecol;
        singlepost['comments']=[];
        var post_id =result[i]. post_id;
        connection.query('SELECT * FROM comment WHERE post_id = ?', [post_id], function (error, results) {
                   if (error) {
                       res.json({
                         status:false,
                         message:'there are some error with query'
                         })
                       }else{

                         singlepost['comments'].push(results)

                           console.log(results);
                    }

                   });

                   allPosts.push(singlepost);

  }

       res.json({
        allPosts
        });

        }

  }
});

但输出是

        {
 "allPosts": [
  {
  "post_id": 1,
  "user_id": 1,
  "post_message": "hi",
  "comments": [

  ]
}
 ]
 }


console.log(results);

然后打印cmets结果

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2019-07-31
    • 2012-08-22
    • 2012-02-07
    • 2022-09-30
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多