Ekrem
Ekrem

Reputation: 405

Firebase Functions response return null

Firebase Function Code

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({
    credential: admin.credential.applicationDefault()
});

const db = admin.firestore();
const gameRef = db.collection('Game');


function newRoom(uid) {
    gameRef.add({
        users: [
            uid
        ],
        playing: false,
        moves: [],
        win: ""
    }).then(ref => {
        return {
            "game": ref.id
        }
    }).catch(err => {
        console.log(err.message)
    })
}

function joinRoom(uid, id, data) {
    data.users.push(uid);
    data.playing = true;
    gameRef.doc(id).update(data)
        .then(ref => {
            return {
                "game": id
            }
        }).catch(err => {
        console.log(err.message);
    })
    ;

}


exports.helloWorlds = functions.https.onCall((data, context) => {
    const uid = context.auth.uid;
    const query = gameRef.where('playing', '==', false).get()
        .then(snapshot => {
            if (snapshot.docs.length === 0) {
                return newRoom(uid)

            } else {
                return joinRoom(uid, snapshot.docs[0].id, snapshot.docs[0].data())
            }
        }).catch(err => {
            console.log(err.message)
        });


});

Android Code

fun  requestGame(text:String): Task<HashMap<*, *>> {
// Create the arguments to the callable function.
val data = hashMapOf("text" to text, "push" to true)
return mFunctions
        .getHttpsCallable("helloWorlds")
        .call(data)
        .continueWith {
            val result = it.result.data as HashMap<*, *>
            result
        }

function code works fine. When I make a request on the android device, it returns null. İt write the datas to the database smoothly. Another problem is that sometimes the function does not work when it is not running for a certain period of time. I think the problem is JavaScript, but I did not solve the problem

Upvotes: 1

Views: 2948

Answers (2)

user1376960
user1376960

Reputation: 1

return gameRef.where(...

exports.helloWorlds = functions.https.onCall((data, context) => {
    const uid = context.auth.uid;
    return gameRef.where('playing', '==', false).get()
        .then(snapshot => {
            if (snapshot.docs.length === 0) {
                return newRoom(uid)

            } else {
                return joinRoom(uid, snapshot.docs[0].id, snapshot.docs[0].data())
            }
        }).catch(err => {
            console.log(err.message)
        });


});

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598688

Right now you're not returning anything from helloWorlds itself, which means that Cloud Functions can't know when it's done. You'll want to return query at the end of helloWorlds:

exports.helloWorlds = functions.https.onCall((data, context) => {
    const uid = context.auth.uid;
    const query = gameRef.where('playing', '==', false).get()
        .then(snapshot => {
            if (snapshot.docs.length === 0) {
                return newRoom(uid)

            } else {
                return joinRoom(uid, snapshot.docs[0].id, snapshot.docs[0].data())
            }
        }).catch(err => {
            console.log(err.message)
        });
    return query;
});

Upvotes: 1

Related Questions