【问题标题】:firebase function converts integer to an objectfirebase 函数将整数转换为对象
【发布时间】:2020-11-01 09:29:50
【问题描述】:

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时,这会导致我的函数崩溃

【问题讨论】:

  • 不是将points 插入字符串格式,而是像这样记录点,以查看更好的字符串表示:console.log(points)
  • 另外,请编辑问题以显示整个云功能。看起来你可能做错了什么。

标签: javascript ios swift firebase google-cloud-functions


【解决方案1】:

您似乎正在将HTTP Cloud Function 实现与Callable Cloud Function 的调用混合使用。两种功能不一样,不兼容。

要从您的应用调用 HTTP 函数,您需要从 Swift 代码执行常规 HTTP 请求。例如How do I make an HTTP request in Swift?

如果你想保持你的 Swift 代码不变,你必须implement your server-side as a Callable Cloud Function。这意味着声明看起来像:

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

你会得到data.userIddata.points等参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-18
    • 2021-12-03
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2020-11-26
    • 2012-05-11
    相关资源
    最近更新 更多