简体   繁体   中英

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

When I'm trying to get data I got undefined, but in one second after recalling the method again I get what I want. How I understand, I just wait for a response and when I try to return an object, I have nothing because it is on his way to me. .findOne() hasn't callback, what I can do in this situation?

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

You might want to take a look at this section of documentation . It says

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

Basically Meteor.subscribe() allows you to include callback which is called when the subscription is ready. It might look like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM