【问题标题】:Firebase function returning null value when called from iOS app从 iOS 应用程序调用时,Firebase 函数返回空值
【发布时间】:2020-08-10 14:11:14
【问题描述】:

我正在从我的 iOS 应用程序调用一个可调用的 firebase 函数,并得到一个 null 的返回值。几天前才返回正确的值,但现在它总是返回 null。数据在返回行之前的控制台中正确记录,并且在 iOS 调用中没有出现错误。

exports.startPlaylist = functions.https.onCall((data, context) => {
    const uid = context.auth.uid;
    const signature = data.signature;

    return axios.post('---url----', {
        data: signature
    }).then(function(response) {
        const val = response.data;

        const ref = database.ref().push();
        ref.set({
            host: {
                uid: uid
            },
            users: {
               uid: uid
            },
            books: val
         }, function(error) {
            if(error) {
                console.log('Not set');
            } else {
                const info = { id: ref.key };
                console.log(info) //Correct log value appears in console
                return info;      //Return null, however
            }
        });
    }).catch(function(err) {
        console.log(err);
    });
 });

Firebase 调用

Functions.functions().httpsCallable("startPlaylist").call(["signature": signature]) { (result, error) in
        guard let result = result, error == nil else { return }
        print(result.data) //<-- prints "null"
 }

【问题讨论】:

  • 你的代码应该使用set()返回的promise来制定响应,而不是你传递给它的错误回调。
  • 我认为print(response.data) 应该是print(result.data) 对吗?
  • @bsod 是的,转换为堆栈时出现拼写错误。结果仍然为空,并且通过了最初的保护声明

标签: node.js swift firebase google-cloud-functions


【解决方案1】:

如上所述,您应该使用 set 返回的承诺:

            ref.set({
                host: {
                    uid: uid
                },
                users: {
                   uid: uid
                },
                books: val
             }).then(function() {
                const info = { id: ref.key };
                console.log(info)
                return info;
             })
             .catch(function(error) {
                  console.log('Not set');
             });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-11
    • 2019-08-12
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2017-10-18
    相关资源
    最近更新 更多