uni-app 云函数无法更新修改
uni-app 云函数无法更新修改
示例代码:
const collection = db.collection("goodsInfo")
const dataBase = await collection.where({
_id: event._id
}).update({
state: event.state
})
return dataBase
操作步骤:
const collection = db.collection("goodsInfo")
const dataBase = await collection.where({
_id: event._id
}).update({
state: event.state
})
return dataBase
预期结果:
能更新值
实际结果:
无法更新值
bug描述:
errCode: InternalServerError | errMsg: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}}
17:51:40.570 Error: errCode: InternalServerError | errMsg: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}}
17:51:40.595 at e.then.catch.e (/tmp/function/@dcloudio/serverless/lib/aliyun/uni-cloud.js:1:1510)
17:51:40.620 at <anonymous>
17:51:40.645 at process._tickCallback (internal/process/next_tick.js:189:7)
更多关于uni-app 云函数无法更新修改的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
兄弟解决了嘛,文档都翻烂了还是报这个错
根据你提供的错误信息,问题在于云函数中更新操作的语法不正确。错误提示明确指出 '$set' is empty,说明更新操作缺少必要的 $set 操作符。
在uni-app云开发中,更新数据库记录需要使用MongoDB的更新操作符。你的代码应该修改为:
const collection = db.collection("goodsInfo")
const dataBase = await collection.where({
_id: event._id
}).update({
$set: {
state: event.state
}
})
return dataBase
关键修改点:
- 在更新对象外层添加
$set操作符 - 要更新的字段放在
$set对象内部
如果 event._id 是字符串格式,而数据库中的 _id 是ObjectId类型,还需要进行类型转换:
const db = uniCloud.database()
const _ = db.command
const collection = db.collection("goodsInfo")
const dataBase = await collection.where({
_id: _.eq(event._id) // 或者使用 db.command.eq
}).update({
$set: {
state: event.state
}
})
return dataBase

