云函数的支付的functionName回调函数中,对执行数据库操作无效,也不能发送请求吗?
发布于 5 年前 作者 qtian 8992 次浏览 来自 官方Issues

云函数的支付的functionName回调函数中,对执行数据库操作无效,也不能发送请求吗?

数据库中表没有新加,请求也没发出去。

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

cloud.init({
  // API 调用都保持和云函数当前所在环境一致
  env: 'pro-wir1p',
  throwOnNotFound: false
})
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  await db.collection('order').add({
    data: {
      isPay: false
    }
  })
  const resultCode = event.resultCode
  const returnCode = event.returnCode
  const orderId = event.outTradeNo

  if (resultCode === 'SUCCESS' && returnCode === 'SUCCESS') {
    // 去数据库里查找 对应的订单,并将对应的状态改为 true
    await db.collection('order').doc(orderId).update({
      data: {
        isPay: true
      }
    })
  try {
    let res = await db.collection('order').doc(orderId).get()
    // 发送请求 https://xxx:3000
    await rp({
      url: 'https://xxx:3000/user/order',
      method: "POST",
      json: true,
      body: res,//这里就是使用的json格式的数据
      headers: {
        "content-Type": "application/json",
      },
    })
    .then(function (res) {
      console.log(res)
    })
    .catch(function (err) {
      console.log('请求失败')
    });
  } catch (error) {
    console.error(error)
  }
    return { errcode: 0 }
  }
}
1 回复

建议数据先写死,调试,然后再接收支付回调信息。

回到顶部