2017-10-09 54 views
2

我有使用findOne的问题,因为它总是返回undefined。React findOne返回undefined在客户端

此代码:

Routine.js

Meteor.methods({ 
    .... // Some lines missing 
    'routines.getRoutine'(routineId) { 
     check(routineId, String); 
     return Routines.findOne(routineId); 
     }, 
}); 

注:如果我做Routines.findOne(routineId)的执行console.log它正确地显示,我正在寻找的元素。

App.jsx

handleSubmit(event) { 
    event.preventDefault(); 

    const comment = ReactDOM.findDOMNode(this.refs.comment).value.trim(); 
    Meteor.call('routines.addComment', this.state.routine._id, comment); 
    let a = Meteor.call('routines.getRoutine', this.state.routine._id); 
    ReactDOM.findDOMNode(this.refs.comment).value = ''; 
    this.setState({ 
     routine: a, 
    }); 
    } 

在我Appjs不要紧,我如何努力 'A' 永远是不确定的,我究竟做错了什么?

感谢您的帮助!

+0

貌似Meteor.call不同步返回一个值,也不会允许状态,通过'routines.addComment更新'。您可以尝试在addComment之后添加超时,以查看它是否在状态更改后定义。 – Scott

回答

2

我很确定你的问题是客户端上的流星调用是异步的,所以你调用的方法在查询相同数据的时候还没有完成。

尝试把代码的其余部分在回调像这样:

handleSubmit(event) { 
    event.preventDefault(); 

    const comment = ReactDOM.findDOMNode(this.refs.comment).value.trim(); 
    Meteor.call('routines.addComment', this.state.routine._id, comment, function() { 
     let a = Meteor.call('routines.getRoutine', this.state.routine._id); 
     ReactDOM.findDOMNode(this.refs.comment).value = ''; 
     this.setState({ 
      routine: a, 
     }); 
    }); 
}