demo_34 评论内容实现_5 对回复的回复的逻辑实现并渲染到页面

效果图:

 1. 修改云函数

'use strict';
const db = uniCloud.database()
const $ = db.command.aggregate
const dbCmd = db.command

exports.main = async (event, context) => {

    const {
        user_id, // 用户id
        article_id, // 文章id
        content, // 评论内容
        comment_id = '' // 评论id,二次评论的时候新添加
    } = event

    let user = await db.collection('user').doc(user_id).get()
    // 获取用户对象
    user = user.data[0]
    // 获取当前的文章信息
    const article = await db.collection('article').doc(article_id).get()
    // 获取文章下的所有评论
    const comments = article.data[0].comments
    // console.log(JSON.stringify(comments))

    let commentObj = {
        comment_id: genID(5), // 评论id
        comment_content: content, // 评论内容
        create_time: new Date().getTime(), // 创建时间
        author: {
            author_id: user._id, // 作者id
            author_name: user.author_name, // 作者名称
            avatar: user.avatar, // 作者头像
            professional: user.professional // 专业
        },
        replys: []
    }

    // 评论文章
    if (comment_id === '') {
        commentObj.replys = []
        commentObj = dbCmd.unshift(commentObj)
    } else {
        // 回复对文章的评论
        // 获取评论索引
        let commentIndex = comments.findIndex(item => item.comment_id === comment_id)
        // 获取作者信息
        let commentAuthor = comments.find(item => item.comment_id === comment_id)
        commentAuthor = commentAuthor.author.author_name
        commentObj.to = commentAuthor // 表示回复谁
        // 更新回复信息
        commentObj = {
            // 在所有评论中找到当前评论
            [commentIndex]: {
                // 更新当前评论的值
                replys: dbCmd.unshift(commentObj)
            }
        }

        /**
         示例: 更新数据中对象的值{name: 2}的值
         let obj = {
             arr:[{name:1},{name:2}]
         } 
         xxx.update({
             arr : {
                 1: {
                     name:3
                 }
             }
         })
         
         */
    }

    // 更新内容到指定文章 不存在则添加字段
    await db.collection('article').doc(article_id).update({
        comments: commentObj
    })

    //返回数据给客户端
    return {
        code: 200,
        msg: "数据更新成功"
    }
}

function genID(length) {
    return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36)
}
 
2. 页面发布功能实现
修复一个报错

 实现

原文地址:https://www.cnblogs.com/luwei0915/p/13677091.html