【四】鸡汤来咯开发学习笔记-84

前端 · 2024-09-07

1.增加功能,使得,看过鸡汤增加阅读次数,选择使用数据库的afterRead功能在 soup-chicken.schema.ext.js中(就是在调用了数据库后发生)

// schema扩展相关文档请参阅:https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html
// article.schema.ext.js
const db = uniCloud.database();
const dbCmd = db.command;

module.exports = {
    trigger: {
        afterRead: async function({   //就是在读取后的事件
            collection,
            operation,
            where,
            field,
            result   //读取后返回的数组
        } = {}) {            
            let ids = result.data.map(item=>item._id); 遍历赋值                
            db.collection("soup-chicken").where({
                _id:dbCmd.in(ids) //在这里面的所有条目
            }).update({                
                view_count:dbCmd.inc(3) //这个inc是自增的意思,在原有基础上增加
            })
        },

        beforeUpdate: async function({
            collection,
            operation,
            docId,
            updateData,
            clientInfo
        } = {}) {
            if (typeof docId === 'string') {
                updateData.last_modify_date = Date.now() // 更新数据的update_date字段赋值为当前服务器时间
            }
        }
    }
}


Theme Jasmine by Kent Liao