最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何返回json评论及其回复评论Nodejs?

IT培训 admin 6浏览 0评论

如何返回json评论及其回复评论Nodejs?

我正在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 
    ]
  }
]

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

回答如下:

获取JSON对象,然后获取该对象的回复对象/数组,然后在此处推送您的评论,然后将修改后的JSON对象拉回

如何返回json评论及其回复评论Nodejs?

我正在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 
    ]
  }
]

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

回答如下:

获取JSON对象,然后获取该对象的回复对象/数组,然后在此处推送您的评论,然后将修改后的JSON对象拉回

发布评论

评论列表 (0)

  1. 暂无评论