【问题标题】:Getting "Function returned undefined, expected Promise or value " while using Firebase Functions使用 Firebase 函数时获取“函数返回未定义、预期的 Promise 或值”
【发布时间】:2021-10-22 07:36:54
【问题描述】:

我最近开始在我的 Unity 游戏中使用 Firebase 来创建一个简单的回合制游戏。 也是第一次使用Firebase函数,更何况JS不熟练。

我正在使用此代码为新添加的玩家处理一个简单的匹配系统到匹配子数据库,并将他们与其他空闲的玩家匹配,然后为游戏创建一个随机 id,将其设置为他们,然后创建“游戏”子数据库中的新对象。

我已将核心上传到 Firebase 并开始通过添加“用户”在实时数据库上手动测试它,但它不起作用,日志显示:

函数返回未定义、预期的 Promise 或值

我在网上查看了该做什么,但我对“承诺”一无所知 非常感谢您在这件事上的帮助。

这是 JS 代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const database = admin.database();

exports.matchmaker = functions.database.ref("matchmaking/{playerId}")
    .onCreate((snap, context) => {
      const gameId = generateGameId();

      database.ref("matchmaking").once("value").then((players) => {
        let secondPlayer = null;
        players.forEach((player) => {
          if (player.val() == "searching" &&
          player.key !== context.params.playerId) {
            secondPlayer = player;
          }
        });

        if (secondPlayer === null) return null;

        database.ref("matchmaking").transaction(function(matchmaking) {
          if (matchmaking === null ||
            matchmaking[context.params.playerId] !== "" ||
            matchmaking[secondPlayer.key] !== "searching") {
            return matchmaking;
          }
          matchmaking[context.params.playerId] = gameId;
          matchmaking[secondPlayer.key] = gameId;
          return matchmaking;
        }).then((result) => {
          if (result.snapshot.child(
              context.params.playerId).val() !== gameId) {
            return null;
          }
          const game = {
            gameInfo: {
              gameId: gameId,
              playersIds: [context.params.playerId, secondPlayer.key],
            },
            turn: context.params.playerId,
          };


          database.ref("games/" + gameId).set(game).then((snapshot) => {
            console.log("Game created successfully!");
            return null;
          }).catch((error) => {
            console.log(error);
          });

          return null;
        }).catch((error) => {
          console.log(error);
        });

        return null;
      }).catch((error) => {
        console.log(error);
      });
    });
/**
 * Generates random game id
 * @return {int} Game id
 */
function generateGameId() {
  const possibleChars =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let gameId = "";
  for (let j = 0; j < 20; j++) {
    gameId +=
    possibleChars.charAt(Math.floor(Math.random() * possibleChars.length));
  }
  return gameId;
}

更新: 我能够通过在 onCreate 方法的末尾添加一个返回值来修复它。

返回上下文。

【问题讨论】:

  • 该错误消息是否有行号?从哪里扔?
  • @Bergi no Emulator log 没有显示,但我想通了。并感谢您的建议。

标签: javascript firebase function


【解决方案1】:

我通过在 onCreate 方法中添加“返回上下文”来修复它。

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2019-08-06
    • 2018-12-03
    • 2019-11-03
    • 2019-08-12
    • 1970-01-01
    相关资源
    最近更新 更多