0
votes

I am making a call to a callable firebase function from my iOS app and am getting a return value of null. The correct value was returned just a few days ago, but now it's always returning null. The data is logging correctly in the console just before the return line, and there is no error appearing within the iOS call.

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 call

Functions.functions().httpsCallable("startPlaylist").call(["signature": signature]) { (result, error) in
        guard let result = result, error == nil else { return }
        print(result.data) //<-- prints "null"
 }
1
Your code should work with the promise returned by set() in order to formulate the response, not an error callback that you pass to it. - Doug Stevenson
I assume print(response.data) should be print(result.data) correct? - liquid
@bsod Yes, typo when converting to stack. Still null result, and gets past the initial guard statement - devaux

1 Answers

0
votes

As stated you should work with the promise returned by 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');
             });