收藏
回答

云函数中调用另一个云函数(获取mqtt的函数)提示缺少参数,错误码-504002?

基础库版本:2.19.2

这个获取mqtt数据的函数是可以在小程序中调用的,当时就是直接传入了全局参数,现在调用就不行了。

// 函数调用
addOrderData:function(event){
    console.log(event);
    console.log('SecretId: ', app.globalData.secretId);
    wx.cloud.callFunction({
      name:'addOrderData',
      data: {
        ProductId: app.globalData.productId,
        DeviceName: app.globalData.deviceName,
        SecretId: app.globalData.secretId,        // 全局变量
        SecretKey: app.globalData.secretKey,
      }
    }).then(res => {
      console.log(res)
    })
}


// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database()
const dishesCollection = db.collection('dishes')
const orderCollection = db.collection('order')

// 云函数入口函数
exports.main = async (event, context) => {
  const { productId, deviceName, secretId, secretKey } = event;
  console.log(productId, deviceName, secretId, secretKey);
  // 获取云端数据
  const mqttData = await cloud.callFunction({
    name: 'iothub-shadow-query',
    data: {
      ProductId: productId,
      DeviceName: deviceName,
      SecretId: secretId,        // 这里提示The request is missing a required parameter 'SecretID'
      SecretKey: secretKey,
    }
  });
  let payload = JSON.parse(mqttData.result.Data); 

  // 获取最新order和_id
  const latestOrder = await orderCollection
    .orderBy('_id', 'desc') // 根据_id(创建时间)降序排序
    .limit(1) // 只取一条数据
    .get(); // 获取数据
  const latestOrderId = latestOrder.data[0]._id; // 保存最新订单的 _id
  console.log(latestOrderId);

  // 数据预处理
  let dishes = {};
  // 遍历 payload.state.reported.order
  for (let i = 0; i < payload.state.reported.order.length; i++) {
    // 使用 `dishes_{num}` 作为键,stateReported.order[i] 作为值
    dishes[`dishes_${i+1}`] = payload.state.reported.order[i];
  }

  return await orderCollection.add({
    data: {
      _id: latestOrderId + 1, 
      ...dishes // 使用展开语法将 dishes 对象的所有键-值对添加到 data 对象中
    }
  })
}

// 报错:
Error: cloud.callFunction:fail Error: errCode: -504002 functions execute fail | errMsg: Error: callFunction:fail -504002 functions execute fail. requestID 1689611537027_0.04395270231172965_33592712-18964b20ddc_1, Error: The request is missing a required parameter `SecretId`.
    at IotcloudClient.parseResponse (:45770/var/user/node_modules/tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client.js:138)
    at processTicksAndRejections (:45770/appservice/internal/process/task_queues.js:97)
    at async IotcloudClient.doRequest (:45770/var/user/node_modules/tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client.js:100)
    at toSDKError (:45770/var/user/node_modules/wx-server-sdk/index.js:8094)
    at Object.returnAsFinalCloudSDKError (:45770/var/user/node_modules/wx-server-sdk/index.js:8065)
    at :45770/var/user/node_modules/wx-server-sdk/index.js:1756
    at processTicksAndRejections (:45770/appservice/internal/process/task_queues.js:97) (callId: 1689611537026-0.7871532857882892) (trace: 0:32:17 start->0:32:18 system error (Error: errCode: -504002 functions execute fail | errMsg: Error: callFunction:fail -504002 functions execute fail. requestID 1689611537027_0.04395270231172965_33592712-18964b20ddc_1, Error: The request is missing a required parameter `SecretId`.
    at IotcloudClient.parseResponse (:45770/var/user/node_modules/tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client.js:138)
    at processTicksAndRejections (:45770/appservice/internal/process/task_queues.js:97)
    at async IotcloudClient.doRequest (:45770/var/user/node_modules/tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client.js:100)
    at toSDKError (:45770/var/user/node_modules/wx-server-sdk/index.js:8094)
    at Object.returnAsFinalCloudSDKError (:45770/var/user/node_modules/wx-server-sdk/index.js:8065)
    at :45770/var/user/node_modules/wx-server-sdk/index.js:1756
    at processTicksAndRejections (:45770/appservice/internal/process/task_queues.js:97)), abort)
    at Ht (WASubContext.js?t=wechat&s=1689601822686&v=2.19.2:2)
    at WASubContext.js?t=wechat&s=1689601822686&v=2.19.2:2(env: macOS,mp,1.06.2306020; lib: 2.19.2)


最后一次编辑于  2023-07-18
回答关注问题邀请回答
收藏

1 个回答

  • Mr.Zhao
    Mr.Zhao
    2023-07-18

    参数写死呢

    2023-07-18
    有用
    回复
登录 后发表内容