如何返回json注释及其回复注释Nodejs?

时间:2019-10-11 13:14:46

标签: mysql node.js json api

我正在Nodejs中进行评论和回复评论功能 我的数据库表包含为image 这是我的代码,显示所有评论及其回复

module.exports.viewCommentOfProduct = (req, res, next)=>{
    Comments.findAll({
        where: {
            productsid: req.params.id
        }
    })
    .then(comments=>{
        res.json({
            comments: comments
        })
    })
    .catch(err=>{
        res.json('Err: '+ err);
    })
}

但是,这不是我所需要的。 我想以树状视图的形式响应JSON

comment: [
 {
    id: 1
    usersid: 
    productsid:
    title:
    content: 
    reply: [
       {id:1, usersid:, title: , content:},
       {id:2, usersid:, title: , content:}
       .... and so on 
    ]
  }
]

我想在显示所有评论时推送所有评论。 您能为我推荐如何解决这个问题吗?

0 个答案:

没有答案