首页 > 解决方案 > firebase 函数将整数转换为对象

问题描述

iOS 客户端调用函数,参数如下:

func buyTicket(contestId: String, points: Int) -> SignalProducer<Void, Error> {
    Functions.functions().httpsCallable("myFunction").call([
        "userId": Auth.auth().currentUser!.uid,
        "points": points,
        "contestId": contestId
        
    ], completion: ...

然后在函数的开头我有这个日志

const userId = req.body.data.userId;
const contestId = req.body.data.contestId;
const points = req.body.data.points;

console.log(`myFunction called with userId: ${userId} contestId: ${contestId} points: ${points}`);

打印出来的是

使用 userId 调用的 myFunction:BzoW5pWLbWRnd2UgjnTkfd3xfNf2 竞赛Id:pBsQo0FHMyu4ay18dexy 点:[object Object]

为什么点转换为对象?当我尝试将点传递给时,这导致我的函数崩溃FieldValue.increment

标签: javascriptiosswiftfirebasegoogle-cloud-functions

解决方案


看起来您将HTTP Cloud Function实现与Callable Cloud Function的调用混合在一起。这两类功能不一样,也不兼容。

要从您的应用程序调用HTTP 函数,您将从 Swift 代码执行常规 HTTP 请求。例如,如何在 Swift 中发出 HTTP 请求?

如果你想让你的 Swift 代码保持不变,你必须将你的服务器端实现为 Callable Cloud Function。这意味着声明看起来像:

exports.myFunction = functions.https.onCall((data, context) => {
  ...
});

你得到的参数为data.userId,data.points等。


推荐阅读