0

我正在编写一个云函数来在 firebase 实时数据库中创建一个节点。该代码似乎正在为所有路径返回一个值。我哪里错了?是否存在gamesRef.push()未调用的情况,这是引发错误的原因吗?

云功能:

export const createGame = functions.https.onCall((data, context) => {

    const game_type = data.game_type;
    if (game_type != GAME_PRACTISE || game_type != GAME_MULTIPLAYER || game_type != GAME_FRIENDS) {
        return {
            "status": 403,
            "id": null
        };
    } else {

        const uid = data.uid;
        const status = GAME_STATUS_OPEN;
        var players = [uid];

        let max_players;
        if (game_type == GAME_PRACTISE) {
            max_players = 1;
        } else if (game_type == GAME_MULTIPLAYER) {
            max_players = 10;
        } else if (game_type == GAME_FRIENDS) {
            max_players = 50;
        }

        let db = admin.database();
        let gamesRef = db.ref("/games");

        gamesRef.push({
            ... // data to be inserted
        }).then(res => {
            return {
                "status": 200,
                "id": gamesRef.key
            }
        }).catch(error => {
            return {
                "status": 403,
                "id": null
            }
        });

    }
});


4

1 回答 1

1

你在return这里缺少一个:

return gamesRef.push({
    ... // data to be inserted
}).then(res => {
    return {
        "status": 200,
        "id": gamesRef.key
    }
}).catch(error => {
    return {
        "status": 403,
        "id": null
    }
});

没有那个顶层return,没有人会看到你的thencatch回调的返回值。

于 2020-07-15T17:31:56.637 回答