2017-10-09 52 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, 
     }); 
    }); 
}