uni-app schema2code生成代码时保存图片失败

uni-app schema2code生成代码时保存图片失败

示例代码:

let formData = {
"ticket_id": null,
"type": "",
"name": "",
"images": null, // bsonType=file
"price": null,
"stock": null,
"status": "true"
}

操作步骤:

"images": {
"bsonType": "file",
"fileMediaType": "image",
"fileExtName": "jpg,png" // 扩展名过滤,多个用 , 分割
},

预期结果:

成功提交图片

实际结果:

保存图片时,报错。 errCode: DATABASE_REQUEST_FAILED | errMsg: [FailedOperation] multiple write errors: [{write errors: [{Cannot create field ‘extname’ in element {images: null}}]}, {nil}]


## bug描述:

schema2code生成表单代码,当bsonType=file时,保存图片报错errCode: DATABASE_REQUEST_FAILED | errMsg: [FailedOperation] multiple write errors: [{write errors: [{Cannot create field 'extname' in element {images: null}}]}, {nil}]

更多关于uni-app schema2code生成代码时保存图片失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app schema2code生成代码时保存图片失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个错误是因为在提交表单时,images字段的值为null,但数据库期望的是一个符合file类型的对象结构。

bsonType设置为file时,数据库期望的存储结构是包含urlnameextname等属性的对象,而不是直接存储null值。

解决方案:

  1. 前端处理:在提交表单前,确保images字段是一个有效的文件对象或空对象,而不是null

    修改你的formData初始化:

    let formData = {
      "ticket_id": null,
      "type": "",
      "name": "",
      "images": {}, // 改为空对象而不是null
      "price": null,
      "stock": null,
      "status": "true"
    }
回到顶部