首页 > 解决方案 > 从 iOS 应用程序调用时,Firebase 函数返回空值

问题描述

我正在从我的 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"
 }

标签: node.jsswiftfirebasegoogle-cloud-functions

解决方案


如前所述,您应该使用 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');
             });

推荐阅读