- 云调用服务端API storage.setUserStorage 报错
云函数中调用cloud.openapi.storage.setUserStorage 代码如下: cloud.openapi.storage.setUserStorage({ openid: update_users.data[i]._id, kvList: [{ key: 'coins', value: update_users.data[i].coins, }, { key: 'solved', value: update_users.data[i].solved.length, }] }) 报错如下: START RequestId:b8d73340-9806-11eb-b6bd-525400be5245 2021-04-08T01:07:00.552Z (node:7) UnhandledPromiseRejectionWarning: Error: errCode: -604104 illegal source of invocation | errMsg: system error: error code: -604104 at callGeneralOpenAPI (/var/user/node_modules/wx-server-sdk/index.js:486:27) at process._tickCallback (internal/process/next_tick.js:68:7) 2021-04-08T01:07:00.552Z (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) 2021-04-08T01:07:00.553Z (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 2021-04-08T01:07:00.553Z (node:7) UnhandledPromiseRejectionWarning: Error: errCode: -604104 illegal source of invocation | errMsg: system error: error code: -604104 at callGeneralOpenAPI (/var/user/node_modules/wx-server-sdk/index.js:486:27) at process._tickCallback (internal/process/next_tick.js:68:7) 2021-04-08T01:07:00.553Z (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) END RequestId:b8d73340-9806-11eb-b6bd-525400be5245 Report RequestId:b8d73340-9806-11eb-b6bd-525400be5245 Duration:203ms Memory:256MB MemUsage:30.187500MB config.json中已配置: "permissions": { "openapi": [ "storage.setUserStorage" ] }, package.json中已配置: "dependencies": { "wx-server-sdk": "~2.4.0" } 开发者工具版本:Stable 1.05.2102010
2021-04-08 - 共享环境不支持数据库事务?
// 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() // 云函数入口函数 exports.main = async (event, context) => { var cloud_back = new cloud.Cloud({ resourceAppid: 'xxx', resourceEnv: 'yyy', }) await cloud_back.init({ env: cloud_back.DYNAMIC_CURRENT_ENV }) const db = cloud_back.database({ throwOnNotFound: false }) const _ = db.command try { const result = await db.runTransaction(async transaction => { const r1 = await transaction.collection('book').doc(event.books[0]._id).get() const r2 = await transaction.collection('book').doc(event.books[1]._id).get() if (r1.data && r2.data) { const u1 = await transaction.collection('book').doc(event.books[0]._id).update({ data: { available_stock: _.inc(1) } }) const u2 = await transaction.collection('book').doc(event.books[1]._id).update({ data: { available_stock: _.inc(1) } }) console.log(`transaction succeeded`) // 会作为 runTransaction resolve 的结果返回 return { r1: r1.data.available_stock + 1, r2: r2.data.available_stock + 1, } } else { // 会作为 runTransaction reject 的结果出去 await transaction.rollback(-100) } }) return { success: true, Account: result, } } catch (e) { console.error(`transaction error`, e) return { success: false, error: e } } } B的环境共享给了A(所有权限) 在A的云函数中,访问B的数据库,使用runTransaction创建事务,抛出异常: { code: 'SIGN_PARAM_INVALID', message: 'you are not authorized to perform operation (tcb:database.startTransaction)' } 如果在A的云函数中,改成访问A的数据库,就没有问题。
2020-12-09