【问题标题】:Firebase return function after Snapshot function is finished快照功能完成后的Firebase返回功能
【发布时间】:2021-08-10 12:07:47
【问题描述】:

我希望我的函数在 ((querySnapshot) => {... 函数完成后返回。 目前,我的noOpponent函数在调用后总是返回false,这可能是因为它在((querySnapshot) => {...函数完成之前返回,所以它在state设置true之前返回。我已经尝试使用var a = await noOpponent(key) 调用 noOpponent 函数,但它不起作用,因为正如我已经写的那样,我想我必须等待 ((querySnapshot) => {... 函数。我该怎么做?

export const noOpponent = (Key) => {
  var state = false;
  console.log("key: " + Key)
  duettsRef
    .where("key", "==", Key)
    .where("player1", "!=", firebase.auth().currentUser.uid)
    .where("player2", "==", "")
    .limit(1)
    .get()
    .then((querySnapshot) => {
     console.log("QuerysnapshotResult: " + !querySnapshot.empty);
     state = true;
      })
      return state;
};

【问题讨论】:

    标签: reactjs firebase react-native google-cloud-firestore google-cloud-functions


    【解决方案1】:

    其他答案建议使用async/await,非常好,推荐代码可读性。

    如果你仍然想使用then(),让我们在这里展示你应该怎么做:

    export const noOpponent = (Key) => {
      var state = false;
      console.log("key: " + Key)
      return duettsRef    // <=== See the return here. More at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#chaining 
        .where("key", "==", Key)
        .where("player1", "!=", firebase.auth().currentUser.uid)
        .where("player2", "==", "")
        .limit(1)
        .get()
        .then((querySnapshot) => {
           console.log("QuerysnapshotResult: " + !querySnapshot.empty);
           state = true;
    
           return state;   // See how we call return in the then block and not outside of it
    
        });
          
    };
    

    请注意,noOpponent() 函数是异步的,并返回一个 Promise。

    【讨论】:

      【解决方案2】:

      这非常适合async/await。它将异步代码转换为同步代码之类的东西。您需要像这样编写代码:

      export const noOpponent = async (Key) => {
        var state = false;
        console.log("key: " + Key)
        const querySnapshot = await duettsRef
          .where("key", "==", Key)
          .where("player1", "!=", firebase.auth().currentUser.uid)
          .where("player2", "==", "")
          .limit(1)
          .get()
          
       console.log("QuerysnapshotResult: " + !querySnapshot.empty);
       state = true;
            
       return state;
      };
      

      确保在调用 noOpponent 时也使用异步或 then。

      
      noOpponent('key').then(state=>{
        console.log('state',state)
      })
      
      

      【讨论】:

        【解决方案3】:

        试试这个代码:

        export const noOpponent = async (Key) => {
          var state = false;
          console.log("key: " + Key)
          let querySnapshot = await duettsRef
            .where("key", "==", Key)
            .where("player1", "!=", firebase.auth().currentUser.uid)
            .where("player2", "==", "")
            .limit(1)
            .get();
            
           // do something like assign value to state then return
              return state;
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-28
          • 2019-05-31
          • 2012-12-14
          相关资源
          最近更新 更多