uni-app uniCloud 数据库更新并返回方法在事务中运行错误 提示findAndUpdate not a function 阿里云
uni-app uniCloud 数据库更新并返回方法在事务中运行错误 提示findAndUpdate not a function 阿里云
产品分类:uniCloud/App
操作步骤:
在事务中使用updateAndReturn提示 findOneAndUpdate not a function
在文档中官网给出的updateAndReturn方法是可以在事务中使用的,但本地运行方法给出如果图的错误提示,但同样的代码使用update方法是没有问题的。
预期结果:
正常可用
实际结果:
TypeError: (intermediate value).findOneAndUpdate is not a function
at ke.updateAndReturn (C:\Program Files\HBuilderX\plugins\unicloud\aliyun\@dcloudio\serverless\lib\aliyun\uni-cloud.js:1:21276)
at Object.module.exports.updateVIPplusPay (c:\My Project\third-street\uniCloud-aliyun\cloudfunctions\admin\mp\updateVIPplusPay.js:23:87)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Object.exports.main (c:\My Project\third-street\uniCloud-aliyun\cloudfunctions\admin\index.js:11:12)
at async uniCloudSecret (C:\Program Files\HBuilderX\plugins\unicloud\aliyun\serve.js:1:5376)
at async module.exports (C:\Program Files\HBuilderX\plugins\unicloud\aliyun\serve.js:1:5031)
at async module.exports.exec (C:\Program Files\HBuilderX\plugins\unicloud\server\controller\cloudfunctions.js:1:1112)
at async Server.<anonymous> (C:\Program Files\HBuilderX\plugins\unicloud\server\index.js:1:1876)
bug描述:
如题所示见附件图片

更多关于uni-app uniCloud 数据库更新并返回方法在事务中运行错误 提示findAndUpdate not a function 阿里云的实战教程也可以访问 https://www.itying.com/category-93-b0.html
连接的是本地云函数吗?HBuilderX版本是多少?https://uniapp.dcloud.net.cn/uniCloud/release?id=_2021-08-27
更多关于uni-app uniCloud 数据库更新并返回方法在事务中运行错误 提示findAndUpdate not a function 阿里云的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3.2.3 ,是本地云函数
根据错误信息,findOneAndUpdate is not a function 表明在事务中调用的 updateAndReturn 方法底层依赖的 findOneAndUpdate 方法不存在。这通常是由于 uniCloud 阿里云服务空间的事务支持版本或本地调试环境的问题导致的。
问题分析:
- 版本兼容性:
updateAndReturn方法需要特定版本的 uniCloud 阿里云基础库支持。请检查你的 uniCloud 阿里云服务空间版本是否过低,建议升级到最新版本。 - 本地调试环境:HBuilderX 的本地调试插件可能未完全支持事务中的
updateAndReturn方法。可以尝试将云函数上传到云端测试,确认是否为本地环境问题。 - 事务方法限制:在事务中,部分数据库操作可能受限于底层实现。虽然文档标明支持,但实际执行时可能因环境差异报错。
解决方案:
- 升级服务空间:在 uniCloud 控制台将阿里云服务空间升级至最新版本(基础库版本建议 ≥ 3.0)。
- 云端测试:将云函数上传到云端运行,排除本地调试环境的影响。
- 替代方案:如果急需解决,可在事务中使用
update更新数据,再通过get方法查询更新后的结果,但需注意事务中查询的一致性。
示例代码调整:
// 原代码(报错)
const res = await transaction.collection('table').updateAndReturn({ ... });
// 替代方案
await transaction.collection('table').update({ ... });
const res = await transaction.collection('table').get(); // 获取更新后数据


