吃饱了不饿
吃饱了不饿
  • 发布:2022-11-01 10:20
  • 更新:2022-11-02 09:34
  • 阅读:231

云数据库权限问题,云函数可以不遵循数据库设计随意添加数据

分类:uniCloud

此贴已结,刚搞懂需要用JQL操作数据库

问题环境:

云空间是阿里云

hbuilder 3.6.4

项目从默认模板创建,vue2 ,勾选了启用unicloud

uni-id-page 从插件市场导入

问题场景:

数据库使用opendb-news-articles.schema表, 约束了需要登陆用户才可以创建数据,
但是小程序开发过程中,测试未登陆用户可以随意添加数据,而且不受表设计的必填约束,defaultValue默认值也没有自动生成。

翻阅文档后未能定位到问题原因,希望有类似问题的能解答一下

下方是数据表db


{  
    "bsonType": "object",  
    "required": [  
        "user_id",  
        "album",  
        "title",  
        "category_id"  
    ],  
    "permission": {  
        "read": "doc.user_id == auth.uid && doc.article_status == 0 || doc.article_status == 1",  
        "create": "auth.uid != null",  
        "update": "doc.user_id == auth.uid",  
        "delete": "doc.user_id == auth.uid"  
    },  
    "properties": {  
        "_id": {  
            "description": "存储文档 ID(用户 ID),系统自动生成"  
        },  
        "user_id": {  
            "bsonType": "string",  
            "description": "文章作者ID, 参考`uni-id-users` 表",  
            "foreignKey": "uni-id-users._id",  
            "defaultValue": {  
                "$env": "uid"  
            }  
        },  
        "category_id": {  
            "bsonType": "string",  
            "title": "分类",  
            "description": "分类 id,参考`uni-news-categories`表",  
            "foreignKey": "opendb-news-categories._id",  
            "enum": {  
                "collection": "opendb-news-categories",  
                "field": "name as text, _id as value"  
            }  
        },  
        "title": {  
            "bsonType": "string",  
            "title": "标题",  
            "description": "标题",  
            "label": "标题",  
            "trim": "both"  
        },  
        "content": {  
            "bsonType": "string",  
            "title": "文章内容",  
            "description": "文章内容",  
            "label": "文章内容",  
            "trim": "right"  
        },  
        "album": {  
            "bsonType": "string",  
            "title": "相册",  
            "description": "相册",  
            "label": "相册",  
            "trim": "both"  
        },  
        "excerpt": {  
            "bsonType": "string",  
            "title": "文章摘录",  
            "description": "文章摘录",  
            "label": "摘要",  
            "trim": "both"  
        },  
        "article_status": {  
            "bsonType": "int",  
            "title": "文章状态",  
            "description": "文章状态:0 草稿箱 1 已发布 2 已删除 3 私密 99 审核未通过",  
            "defaultValue": 0,  
            "enum": [  
                {  
                    "value": 0,  
                    "text": "草稿箱"  
                },  
                {  
                    "value": 1,  
                    "text": "已发布"  
                },  
                {  
                    "value": 2,  
                    "text": "已删除"  
                },  
                {  
                    "value": 3,  
                    "text": "私密"  
                },  
                {  
                    "value": 99,  
                    "text": "审核未通过"  
                }  
            ]  
        },  
        "view_count": {  
            "bsonType": "int",  
            "title": "阅读数量",  
            "description": "阅读数量",  
            "permission": {  
                "write": false  
            }  
        },  
        "like_count": {  
            "bsonType": "int",  
            "description": "喜欢数、点赞数",  
            "permission": {  
                "write": false  
            }  
        },  
        "is_sticky": {  
            "bsonType": "bool",  
            "title": "是否置顶",  
            "description": "是否置顶",  
            "permission": {  
                "write": false  
            }  
        },  
        "is_essence": {  
            "bsonType": "bool",  
            "title": "阅读加精",  
            "description": "阅读加精",  
            "permission": {  
                "write": false  
            }  
        },  
        "comment_status": {  
            "bsonType": "int",  
            "title": "开放评论",  
            "description": "评论状态:0 关闭  1 开放",  
            "enum": [  
                {  
                    "value": 0,  
                    "text": "关闭"  
                },  
                {  
                    "value": 1,  
                    "text": "开放"  
                }  
            ]  
        },  
        "comment_count": {  
            "bsonType": "int",  
            "description": "评论数量",  
            "permission": {  
                "write": false  
            }  
        },  
        "last_comment_user_id": {  
            "bsonType": "string",  
            "description": "最后回复用户 id,参考`uni-id-users` 表",  
            "foreignKey": "uni-id-users._id"  
        },  
        "avatar": {  
            "bsonType": "string",  
            "title": "封面大图",  
            "description": "缩略图地址",  
            "label": "封面大图",  
            "trim": "both"  
        },  
        "publish_date": {  
            "bsonType": "timestamp",  
            "title": "发表时间",  
            "description": "发表时间",  
            "defaultValue": {  
                "$env": "now"  
            }  
        },  
        "publish_ip": {  
            "bsonType": "string",  
            "title": "发布文章时IP地址",  
            "description": "发表时 IP 地址",  
            "forceDefaultValue": {  
                "$env": "clientIP"  
            }  
        },  
        "last_modify_date": {  
            "bsonType": "timestamp",  
            "title": "最后修改时间",  
            "description": "最后修改时间",  
            "defaultValue": {  
                "$env": "now"  
            }  
        },  
        "last_modify_ip": {  
            "bsonType": "string",  
            "description": "最后修改时 IP 地址",  
            "forceDefaultValue": {  
                "$env": "clientIP"  
            }  
        },  
        "mode": {  
            "bsonType": "number",  
            "title": "排版显示模式",  
            "description": "排版显示模式,如左图右文、上图下文等"  
        }  
    },  
    "version": "0.0.1"  
}
2022-11-01 10:20 负责人:无 分享
已邀请:
吃饱了不饿

吃饱了不饿 (作者) - 吃饱了不饿

新增文章的云函数如下,token校验是自己添加的

'use strict';  
const db = uniCloud.database()  

exports.main = async (event, context) => {  
    //event为客户端上传的参数  
    console.log('event : ', event)  
    console.log('1111111 : ', context)  

    // 校验token是否传递  
    // const userInfo = await uniID.check(event.uniIdToken)  
    if (!event.uniIdToken) {  
        return {  
            errCode: "TOKEN_IS_NULL",  
            errMsg: 'Token未获取,请检查登陆状态'  
        }  
    }  
    const idCommonToken = uniCloud.importObject('id-common-token')  
    const res111 = await idCommonToken.checkToken(event.uniIdToken)  

    console.log(res111)  

    // 构造默认参数  
    const params = {  
        article_status: 1,  
        ...event  
    }  

    const collection = db.collection('opendb-news-articles')  
    const res = await collection.add(params)  
    return res  

    //返回数据给客户端  
    // return event  
};  
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

schema仅对jql语法生效。云函数里面使用原始的数据库语句是不会使用schema进行校验的

魑魅魍魉cmwl

魑魅魍魉cmwl

云函数里要用JQL应该使用uniCloud.databaseForJQL()吧

要回复问题请先登录注册