2017-04-03 12 views
0

以下のAltアクションコードで、addCharacterの成功後にgetCharacters()を呼び出すにはどうすればよいですか?基本的に私は新しいレコードをデータベースに保存した後に文字のリストをリフレッシュしようとしています。React Js ALT - 他の関数の成功時に関数を呼び出す

getCharacters() { 
    requestPromise('http://localhost:3100/api/characters') 
     .then((res) => { 
     console.log("Getting Characters"); 
     var opdata = JSON.parse(res); 
     this.actions.getCharactersSuccess(opdata.characters); 
     }).catch((err) => { 
     console.log('error:', err); 
     this.actions.getCharactersFail(err) 
     }) 
    } 

    addCharacter(data) { 
    var options = { 
     method: 'POST', 
     uri: 'http://localhost:3100/api/characters/add/', 
     json: true, 
     body: { 
     name: data.charName, 
     allegiance: data.charAllegiance, 
     }, 
    }; 
    requestPromise(options) 
     .then((res) => { 
      // How can I recall getCharacters() from here 
     }).catch((err) => { 
     console.log('error:', err); 
     }) 
    } 

STORE

getCharactersSuccess(res) { 
    this.setState({ 
     characters: res 
    }) 
    } 
+0

チェックこの1、役に立つかもしれません。http://stackoverflow.com/questions/34370957/bluebird-warning-a-promise-was-created-in-a-handler-but-was-not-返信from私は –

答えて

0

によってこれが良いだろうright.Itを見ていないことを行うことができます。あなたがそれをすることができます任意の方法は、return this.getCharacters();を呼び出すだけ購入する。クラス内のgetCharacters()関数を参照します。あなたが逃した要点はです。は正しい参照を選択します。また、約束の範囲内で約束を作成するとエラーが発生しますが、その約束のスコープから何も返さない。だから返信あなたの問題を解決します。

addCharacter(data) { 
    var options = { 
     method: 'POST', 
     uri: 'http://localhost:3100/api/characters/add/', 
     json: true, 
     body: { 
     name: data.charName, 
     allegiance: data.charAllegiance, 
     }, 
    }; 
    requestPromise(options) 
     .then((res) => { 
     return this.getCharacters(); 
     }).catch((err) => { 
     console.log('error:', err); 
     }) 
    } 
+0

私はAltJを使用しています。このソリューションを試しましたが、エラーが発生しました:ハンドラで約束が作成されましたが、返されませんでした – James

+0

this.getCharacters()の前にreturnキーワードを追加してみてください。 – TRomesh

+0

@James私は私の答えと説明を編集しました – TRomesh

0

あなただけgetCharacters関数を呼び出す必要があります。あなたは、フラックスやReduxのような任意のアーキテクチャを使用している場合this.getCharacters()

addCharacter(data) { 
    var options = { 
     method: 'POST', 
     uri: 'http://localhost:3100/api/characters/add/', 
     json: true, 
     body: { 
     name: data.charName, 
     allegiance: data.charAllegiance, 
     }, 
    }; 
    requestPromise(options) 
     .then((res) => { 
     return this.getCharacters(); 
     }).catch((err) => { 
     console.log('error:', err); 
     }) 
    } 
+0

私はこれを試みたが、エラーを受け取る:約束はハンドラで作成されたが、それから返されなかった – James

+0

あなたはエラーではなく、警告を受け取る。 'return this.getCharacters()'を追加するだけです。 – juancab

+0

@Jamesなぜあなたはその編集が必要ですか –

関連する問題