简体   繁体   English

异步 function 本机反应

[英]Async function react native

We have to create a Bingo game in React Native with Firebase Realtime Database on Android simulator.我们必须在 Android 模拟器上使用 Firebase 实时数据库在 React Native 中创建宾果游戏。 The app game is for 2 players.该应用程序游戏适合 2 位玩家。 When the first player enter in the app, he create the game and wait for the second player to join.当第一个玩家进入应用程序时,他创建游戏并等待第二个玩家加入。 we want to create a screen with the writing: "Waiting for another player" that appears to the first player until the second player connects then when the second player connects the card is shown.我们想创建一个屏幕,上面写着:“等待另一位玩家”出现在第一个玩家,直到第二个玩家连接,然后当第二个玩家连接卡时显示。

We wrote this code but it return 'undefined'.我们编写了这段代码,但它返回“未定义”。

function Game(){

  const authCtx = useContext(AuthContext);

  const gameCtx = useContext(GameContext);

  const [loadPlayer, setLoadPlayer] = useState(false);



  useEffect(() => {

    async function gamePlay(){

      gameCtx.player1 = authCtx.token;

      const play = await setGame(authCtx.token, gameCtx);

      console.log(play); //return undefined 

      if(play == 'CREATE'){

        setLoadPlayer(true);

      }else if(play == 'UPDATE'){

        setLoadPlayer(false);

      }

      if(loadPlayer){

        return <LoadingOverlay message="Waiting for another player... " />;

      }

    }

    gamePlay();

  }, []);



  return <Card />;

}



export default Game;
export function create(game){
    const db = getDatabase();
    const newGameKey = push(child(ref(db), 'GAME')).key;
    set(ref(db, '/GAME/' + newGameKey), game)
    .then(() => {console.log('Game Create!');})
    .catch((error) => {console.log(error);});
}

export function setGame(email, game){
    const dbRef = ref(getDatabase());
    var player = false;
    get(child(dbRef, 'GAME/')).then((snapshot) => {
        if (snapshot.exists()) {
            snapshot.forEach(function(childSnapshot) {
                const key = childSnapshot.key;
                const key1 = snapshot.child(key + '/player1').val();
                const key2 = snapshot.child(key + '/player2').val();
                if( key2 == "" && email != key1){
                    console.log('P2');
                    updateGame(email, key);
                    player = true;
                    return true;
                }
            });
            if(player == false){
                console.log('P1');
                player = true;
                create(game);
            }
        } else {
            //create the first game!
            create(game);
        }
    }).catch((error) => {
        console.error(error);
    });
}

export function updateGame(email, key){
    console.log('Update: ' + key);
    const db = getDatabase();
    const updates = {};
    updates['/GAME/' + key + '/player2'] = email;
   return update(ref(db), updates);
}

We think this is due to "async" and "await" because not working correctly.我们认为这是由于“async”和“await”无法正常工作所致。

Do you have any suggestions?你有什么建议吗? How can we redirect the first player to a waiting screen?我们如何将第一个玩家重定向到等待屏幕?

is ref(getDatabase()) is promise?. ref(getDatabase())是 promise 吗? if it is then use await before it.如果是,则在它之前使用 await 。

and use async function before setGame if you are using await while calling.如果您在调用时使用 await,则在 setGame 之前使用 async function。

export async function setGame(email, game){
        const dbRef = await ref(getDatabase());
        var player = false;
        get(child(dbRef, 'GAME/')).then((snapshot) => {
            if (snapshot.exists()) {
                snapshot.forEach(function(childSnapshot) {
                    const key = childSnapshot.key;
                    const key1 = snapshot.child(key + '/player1').val();
                    const key2 = snapshot.child(key + '/player2').val();
                    if( key2 == "" && email != key1){
                        console.log('P2');
                        updateGame(email, key);
                        player = true;
                        return true;
                    }
                });
                if(player == false){
                    console.log('P1');
                    player = true;
                    create(game);
                }
            } else {
                //create the first game!
                create(game);
            }
        }).catch((error) => {
            console.error(error);
        });
    }

暂无
暂无

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

相关问题 处理异步等待将 Firebase 与本机反应一起使用 - Handling Async Await to use Firebase with react native 已经安装了 @react-native-async-storage/async-storage 并安装了 expo 但贬值警告不会 go 离开 - Already installed @react-native-async-storage/async-storage with expo install but deprication warning won't go away React-native 应用程序:navigation.navigate 不是 function - React-native app: navigation.navigate is not a function 从 React-Native 调用 AWS Lambda 函数 - Call AWS Lambda function from React-Native firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app? React Native Firebase 注销 function 在根据用户类型登录后不起作用 - React Native Firebase logout function doesn't work after login based on user type 无法从 React-Native-Firebase(v6) Firestore 获取数据:undefined 不是函数(靠近“...this._firestore.native.collectionGet...”) - Can't get data from React-Native-Firebase(v6) Firestore: undefined is not a function (near '...this._firestore.native.collectionGet...') 使用 Firebase 在 React Native Expo 应用程序中验证登录页面,但必须单击按钮两次才能执行完整的 function - Using Firebase to Authenticate login page in a React Native Expo App but have to click the button twice for the full function to execute Firebase 是 react-native 后端 - Firebase are react-native backend React Native 的 AuthProvider 操作失败? - AuthProvider operation fails for React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM