2017-04-03 45 views
0

在下面的Alt Actions代碼中,如何在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

檢查這一個,可能會有所幫助:http://stackoverflow.com/questions/34370957/bluebird-warning-a-promise-was-created-in-a-handler-but-was-not-從我 –

回答

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

我正在使用AltJs。我已經嘗試過這個解決方案,但收到錯誤:在處理程序中創建了一個承諾,但沒有從它返回 – James

+0

嘗試在this.getCharacters()前添加return關鍵字; – TRomesh

+0

@James我編輯了我的答案和描述 – TRomesh

0

你只需要調用getCharacters功能。你可以做到這一點的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爲什麼你需要編輯 –

相關問題