defaultValue和forceDefaultValue在uni-app中设置无效

defaultValue和forceDefaultValue在uni-app中设置无效

操作步骤:

{
"bsonType": "object",
"required": [
"name"
],
"permission": {
"read": true,
"create": true,
"update": true,
"delete": true
},
"properties": {
"_id": {
"description": "存储文档 ID(文章 ID),系统自动生成"
},
"parent_id": {
"bsonType": "string",
"description": "父ID,用于多级分类"
},
"name": {
"bsonType": "string",
"description": "类别名称",
"title": "类别名称",
"trim": "both"
},
"icon": {
"bsonType": "string",
"description": "类别图标/图片地址",
"title": "图标地址",
"trim": "both"
},
"sort": {
"bsonType": "int",
"description": "类别排序,越大越靠后",
"title": "排序"
},
"description": {
"bsonType": "string",
"description": "类别描述",
"title": "类别描述",
"trim": "both"
},
"is_hot_show": {
"bsonType": "bool",
"title": "加入热门显示",
"description": "是否热门显示"
},
"is_index_show": {
"bsonType": "bool",
"title": "首页显示",
"description": "是否首页显示"
},
"create_date": {
"bsonType": "timestamp",
"description": "创建时间",
"forceDefaultValue": {
"$env": "now"
}
},
"userUid":{
"bsonType": "string",
"forceDefaultValue": {
"$env": "uid"
}
}
},
"version": "0.0.1"
}

预期结果:

数据库每新增数据时create_date的forceDefaultValue设置成功


# 实际结果:

数据库每新增数据时create_date的forceDefaultValue设置不成功

bug描述:

schema设置forceDefaultValue和defaultValue无效,当提交数据不传参数,设置的默认值无效


![](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20250606/b7e32ca08f46d9712f200011f6f8b6af.jpg)

![](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20250606/ed1f399c7ce22eb0ea4434ad8e7392be.jpg)

更多关于defaultValue和forceDefaultValue在uni-app中设置无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

先说结论:此非bug,schema本身仅对JQL语句生效。
原因:JQL是unICloud官方在传统db的基础上封装的,并不是数据库本身的功能,只有通过代码 const dbJQL = uniCloud.databaseForJQL 获得的 dbJQL 对象去操作数据库才受到 schema 的影响。

更多关于defaultValue和forceDefaultValue在uni-app中设置无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html


根据你提供的schema配置和截图来看,forceDefaultValue设置无效的问题可能有以下几个原因:

  1. 确保使用的是最新版uniCloud客户端SDK和云端服务,旧版本可能存在兼容性问题

  2. 检查schema是否已正确上传到uniCloud控制台并生效。修改schema后需要重新上传才会生效

  3. 确认客户端提交数据时没有显式传递这些字段值。如果手动传了空值会覆盖默认值

  4. 对于timestamp类型,可以尝试改为以下格式:

"create_date": {
  "bsonType": "timestamp",
  "defaultValue": {
    "$env": "now"
  }
}
回到顶部