【问题标题】:Firebase Functions response return nullFirebase 函数响应返回 null
【发布时间】:2018-11-24 18:03:29
【问题描述】:

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


});

安卓代码

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,但我没有解决问题

【问题讨论】:

    标签: android node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案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)
            });
    
    
    });
    

    【讨论】:

      【解决方案2】:

      现在您没有从 helloWorlds 本身返回任何内容,这意味着 Cloud Functions 无法知道它何时完成。你会想return queryhelloWorlds结尾:

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

      【讨论】:

      • 查询始终为空。
      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2021-03-04
      相关资源
      最近更新 更多