繁体   English   中英

Firebase函数响应返回null

[英]Firebase Functions response return null

Firebase功能代码

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代码

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
        }

功能代码可以正常工作。 当我在android设备上发出请求时,它返回null。 请将数据平稳地写入数据库。 另一个问题是,有时该功能在一段时间内未运行时将无法正常工作。 我认为问题是JavaScript,但我没有解决问题

现在,您不会从helloWorlds本身返回任何信息,这意味着Cloud Functions无法确定何时完成。 您需要在helloWorlds的末尾return query

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;
});

返回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)
        });


});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM