uni-app 三个链表查询在云对象中报错没有权限

发布于 1周前 作者 h691938207 来自 Uni-App

uni-app 三个链表查询在云对象中报错没有权限

示例代码:

const res = await this.db.collection('inventory,uni-id-users,returner').where(`state==${state}`).field(`name,id,weight,from,cpeople.nickname,cpoeple._id,outpeople.nickname,outpeople._id,created_at,_id.weight,_id.inventory_id`).get() // 直接关联order和book之后再过滤  
// const inventory = this.db.collection('inventory').where('state==2').get()  
// const res = await this.db.collection(inventory, 'uni-id-users').field('nickname').get() // 将获取的order表的临时表和book表进行联表查询  
return {code:200,data:res.data}  

returner表

{  
    "bsonType": "object",  
    "required": ["inventory_id"],  
    "permission": {  
        "read": true,  
        "create": true,  
        "update": true,  
        "delete": true,  
        "count": true  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成",  
            "order": 0  
        },  
        "inventory_id": {  
            "bsonType": "string",  
            "label": "库存id",  
            "order": 1,  
            "trim": "both",  
            "permission": {  
                "read": true  
            }  
        },  
        "info": {  
            "bsonType": "object",  
            "label": "信息",  
            "errorMessage": {  
                "format": "{label}格式无效"  
            },  
            "order": 2  
        },  
        "weight": {  
            "bsonType": "double",  
            "label": "退货重量",  
            "errorMessage": {  
                "format": "{label}格式无效"  
            },  
            "permission": {  
                "read": true  
            },  
            "order": 3  
        },  
        "state": {  
            "bsonType": "int",  
            "label": "状态",  
            "errorMessage": {  
                "format": "{label}格式无效"  
            },  
            "order": 4,  
            "description": "1申请中2申请成功",  
            "exclusiveMinimum": true,  
            "exclusiveMaximum": true  
        }  
    }  
}

inventory表

{  
    "bsonType": "object",  
    "required": ["name", "id", "cweight", "from", "cpeople", "created_at"],  
    "permission": {  
        "read": true,  
        "create": true,  
        "update": true,  
        "delete": true,  
        "count": true  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成",  
            "order": 0,  
            "foreignKey": "returner.inventory_id"  
        },  
        "name": {  
            "bsonType": "object",  
            "label": "规格名称",  
            "order": 1  
        },  
        "id": {  
            "bsonType": "string",  
            "label": "批号",  
            "order": 2,  
            "trim": "both"  
        },  
        "weight": {  
            "bsonType": "double",  
            "label": "材料重量",  
            "order": 3,  
            "exclusiveMinimum": true,  
            "exclusiveMaximum": true  
        },  
        "from": {  
            "bsonType": "object",  
            "label": "来源单位",  
            "order": 4  
        },  
        "cpeople": {  
            "bsonType": "string",  
            "label": "入库人",  
            "forceDefaultValue": {  
                "$env": "uid"  
            },  
            "foreignKey": "uni-id-users._id",  
            "order": 5,  
            "trim": "both"  
        },  
        "outpeople": {  
            "bsonType": "string",  
            "label": "出库人",  
            "order": 8,  
            "foreignKey": "uni-id-users._id",  
            "trim": "both"  
        },  
        "state": {  
            "bsonType": "int",  
            "label": "状态",  
            "order": 9,  
            "description": "1入库2出库审核3出库4退货审核5退货",  
            "exclusiveMinimum": true,  
            "exclusiveMaximum": true,  
            "defaultValue": 1  
        },  
        "created_at": {  
            "bsonType": "timestamp",  
            "label": "创建日期",  
            "forceDefaultValue": {  
                "$env": "now"  
            },  
            "order": 10  
        },  
        "jian": {  
            "bsonType": "int",  
            "label": "副单位卷",  
            "order": 12,  
            "exclusiveMinimum": true,  
            "exclusiveMaximum": true  
        }  
    }  
}

操作步骤:

  • 运行代码

预期结果:

[
{
"_id": [],
"cpeople": [
{
"nickname": "asd1"
}
],
"created_at": 1701249953256,
"from": {
"_id": "65655eb9862066d0706e6781",
"bfullname": "张三供应商",
"btypeid": "0000100001",
"id": "0000100001",
"name": "张三供应商"
},
"id": "1701249953200",
"name": {
"_id": "651be05c55b3379a66938ef4",
"id": "000010000100001",
"name": "8.6*0.1",
"pfullname": "8.6*0.1",
"price": 2,
"ptypeid": "000010000100001"
},
"outpeople": [
{
"_id": "64f1e75ae0ec19bea11ec476",
"nickname": "asd1"
}
],
"weight": 2
}
]

实际结果:

{
    "objectName": "model",
    "methodName": "getReturnReceivelist",
    "params": Array(1),
    "error": "Error: 权限校验未通过,请参考文档:https://uniapp.dcloud.net.cn/uniCloud/schema.html#handler-permission-error"
}

bug描述:

三个链表查询报错权限错误,删掉returner表就可以了,returner表中的权限是true,不明白什么权限问题


1 回复

uni-app 中使用云对象进行数据库操作时,如果遇到“没有权限”的错误,通常是由于云函数的权限配置不正确导致的。以下是一些可能的原因和解决方法:

1. 检查云函数的权限配置

uniCloud 中,云函数的权限配置是通过 cloudfunctions 目录下的 config.json 文件来控制的。确保你的云函数有足够的权限来访问数据库。

{
  "permissions": {
    "openapi": [],
    "database": {
      "collection": {
        "your_collection_name": ["read", "write"]
      }
    }
  }
}

确保 your_collection_name 是你需要访问的集合名称,并且权限设置为 readwrite

2. 检查云对象的权限

如果你使用的是云对象(cloudobject),确保云对象的权限配置正确。云对象的权限配置与云函数类似,也需要在 config.json 中设置。

3. 检查数据库集合的权限

uniCloud 控制台中,检查你正在访问的数据库集合的权限设置。确保集合的权限允许云函数或云对象进行读取和写入操作。

4. 检查云函数的调用方式

确保你在 uni-app 中正确调用了云函数或云对象。例如:

const cloudObject = uniCloud.importObject('your_cloud_object_name');
cloudObject.yourMethod().then(res => {
  console.log(res);
}).catch(err => {
  console.error(err);
});

5. 检查云函数的日志

uniCloud 控制台中查看云函数的日志,获取更详细的错误信息。日志可以帮助你定位问题的具体原因。

6. 确保云函数已部署

确保你的云函数或云对象已经成功部署到云端。未部署的云函数无法被调用。

7. 检查云函数的代码

确保云函数或云对象的代码没有错误,并且正确处理了数据库查询。例如:

const db = uniCloud.database();
const collection = db.collection('your_collection_name');

exports.main = async (event, context) => {
  const res = await collection.get();
  return res;
};
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!