react-native-meteor Meteor.collection('xxx').findOne() return 未定义,如何回调

react-native-meteor Meteor.collection('xxx').findOne() return undefined, how to make a callback

当我试图获取数据时,我得到了未定义的数据,但是在再次调用该方法后的一秒钟内,我得到了我想要的。我的理解是,我只是等待响应,当我尝试 return 一个对象时,我什么都没有,因为它正在向我发送。 .findOne()没有回调,这种情况怎么办?

  handleLogin = () => {
      Meteor.loginWithPassword(this.state.loginField,this.state.passwordField,(error)=>{
          if (!error) {
          Meteor.subscribe('xxx')
            let data = Meteor.collection('xxxy').findOne();
            console.log(data);
          }
      }
  }

您可能想看看 documentation 的这一部分。它说

The onReady callback is called with no arguments when the server marks the subscription as ready.

基本上 Meteor.subscribe() 允许您包含订阅准备就绪时调用的回调。它可能如下所示。

Meteor.subscribe('xxx', function () {
  const data = Meteor.collection('xxxy').findOne()
  console.log(data)
})