Node.Js 从 POST 请求中获取数据

标签 node.js swift firebase google-cloud-functions stripe-payments

我正在使用 Firebase 云函数。在 iOS 设备上,我正在部署触发器以在 Node.Js 中运行云功能。在 Xcode 中——这是我触发云功能的客户端函数——我正在传递字典 data

func updateTermsOfServiceCloudCall(){

    let data = [
        "accountId": "acct_1Ew#######"
    ]

    Functions.functions().httpsCallable("updateAccountWithTOA").call(data) { (result, error) in
 }

现在在 Node 中,我正在运行此代码以部署到 Firebase 云

exports.updateAccountWithTOA = functions.https.onRequest((request, response) => {


const data = request.body;
const accoundId = data.accoundId;

stripe.accounts.update(

    accoundId,
    {
        tos_acceptance: {
          date: Math.floor(Date.now() / 1000),
          ip: request.ip 
        }
      },
    )

});

我希望获得从我的 iOS 客户端传递过来的字典数据。但是,我在获取 Node 中的数据时遇到了问题。我以为 request.body 会给我数据,但我猜我错了,因为我收到了这个错误。任何帮助或建议将不胜感激。谢谢

Error: Stripe: Argument "id" must be a string, but got: undefined (on API request to POST /accounts/{id})

最佳答案

您的客户端代码正在尝试调用 callable function ,但您的函数定义为 HTTP type function .它们是不同的东西。您无法使用 Functions SDK 调用常规 HTTP 函数。

如果要使用Functions SDK 来调用函数,则需要使用onCall 而不是onRequest 来定义它,如documentation for callable functions 所示。 .或者,如果您不想在后端调用,则需要使用 HTTP 客户端库调用常规 HTTP 函数。

关于Node.Js 从 POST 请求中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57083275/

相关文章:

javascript - 在单独的 js 文件中处理所有带有前缀的路由

node.js - 创建后 Postgresql 数据库在 travis ci 中不存在

ios - 从 tableView 部分创建图像

Firebase 聊天 - 删除旧消息

javascript - 带有 TypeScript 的 npm 模块

node.js - auth0 回调在登录 angular2 后不设置配置文件日期

ios - 其他 View 大小在自动布局中自动更改为零

ios - 应用程序卡住直到将数据保存到核心数据中

android - Firebase Cloud Messaging Service - 即使应用程序被杀死也能收到通知

node.js - Firebase 功能 : if value exists, 行动然后删除