uni-app file格式的字段不能入库

uni-app file格式的字段不能入库

操作步骤:

  • 正常提交

预期结果:

  • 应能正确入库

实际结果:

  • 不能入库

bug描述:

DB schemak中有个字段

"goodsUrl": {
    "title": "商品照片",
    "description": "商品照片",
    "bsonType": "file",
    "fileMediaType":"image",
    "fileExtName":"jpg,png",
    "maxLength": 1
}

用的schema2code生成的add页面,提交时提示 “商品照片格式不匹配”

商品照片


更多关于uni-app file格式的字段不能入库的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

“bsonType”: “file” 时 maxLength 是无效的,“bsonType”: “array” 时才有效,但是仍然会走校验 先去掉 maxLength,后面会优化校验逻辑

更多关于uni-app file格式的字段不能入库的实战教程也可以访问 https://www.itying.com/category-93-b0.html


我没有maxLength,也遇到了相同的问题: DB schemak中 “image”: { “bsonType”: “file”, “fileMediaType”: “image”, “title”: “文件”, “description”: “文件” },
提交的数据 [{ “name”: “9gOywDC1QqIuc2cae5e1777d87bce9272abdcdad1f73.jpg”, “extname”: “jpg”, “fileType”: “image”, “url”: “https://vkceyugu.cdn.bspapp.com/VKCEYUGU-61258ee6-76ce-4569-82f4-718cf3104412/8f58b63b-24ad-4477-ae1a-73ab626a7e96.jpg”, “size”: 173888, “image”: { “width”: 915, “height”: 664, “location”: “http://tmp/9gOywDC1QqIuc2cae5e1777d87bce9272abdcdad1f73.jpg” }, “category”: “avatar” }]
返回错误: “VALIDATION_ERROR: 数据库验证失败:[“image”]类型无效”

根据问题描述,这是uniCloud数据库schema中file类型字段的常见问题。以下是解决方案:

  1. 问题原因:
  • file类型字段需要特殊处理,不能直接提交文件路径
  • 需要先调用uniCloud.uploadFile上传文件,获取fileID后再提交
  1. 修改建议:
// 上传文件示例
uniCloud.uploadFile({
  filePath: tempFilePath, // 临时文件路径
  cloudPath: 'goods/' + Date.now() + '.jpg', // 云端路径
  success: (res) => {
    // 获取fileID后提交表单
    this.formData.goodsUrl = res.fileID
    this.submitForm()
  }
})
回到顶部