2017-06-12 194 views
0

我正在試驗REDX傳奇,我有一個問題試圖從我的API獲取數據。我改變了我的API,我使用fetchjs,並遇到了一些麻煩。如何使用Fetch API將數據返回給調用函數?

問題是數據沒有被返回到調用函數,但API正在接收數據。

這裏是我的傳奇代碼:

/* eslint-disable no-constant-condition */ 

import { put, call, takeEvery } from 'redux-saga'; 
import { delay } from 'redux-saga'; 
import RPC from "../../RPC"; 

export function* load_contacts() { 
    console.log("Called"); // This part is printed 
    try{ 
     const data = yield call(RPC.execute("contact","search_read_path",[[],["name","picture"]],{})); // *this is the calling function. nothing is returned. 
     console.log("waiting for data",data); // this is not printed 
     yield put({type:"CONTACTS_LOADED",payload:data}); 
    } 
    catch (e) { 
      yield put({type: "CONTACTS_LOAD_FAILED", message: e.message}); 
    } 
} 

export default function* rootSaga() { 
    console.log(">> rootSaga"); 
    yield takeEvery('LOAD_CONTACTS', load_contacts) 
} 

這裏是我的API代碼:

module.exports.execute=function(model,method,args,opts,cb) { 
    console.info("rpc.execute",model,method,args,opts); 
    if (!_rpc_base_url) throw "RPC base url is undefined"; 
    var params=[model,method]; 
    params.push(args); 
    if (opts) { 
     params.push(opts); 
    } 
    var headers={ 
     "Accept": "application/json", 
     "Content-Type": "application/json", 
    }; 
    if (_database) headers["X-Database"]=_database; 
    if (_schema) headers["X-Schema"]=_schema; 

    fetch(_rpc_base_url+"/json_rpc",{ 
     method: "POST", 
     headers: headers, 
     body: JSON.stringify({ 
      id: (new Date()).getTime(), 
      method: "execute", 
      params: params 
     }), 
    }) 
    .then((response) => response.json()) 
    .then((data)=>{ 
     console.log(data); // data is printed here 
     return data} // doesnot return 
    ) 
    .catch((err) => { 
     alert("upload error: "+err); 
     if (result_cb) result_cb(err,null); 
    }) 
}; 

我相信這是與我使用取JS(不正確的方法)。我找不到任何可能有幫助的解決方案。使用回調

如果UPDATE,

export function* load_contacts() { 
    console.log("Called") 
    try{ 
     const data ={}; 
     yield call(RPC.execute("contact","search_read_path",[[],["name","picture"]],{},(err,data)=>{ 
      if (err){ 
       alert(err); 
       return 
      } 
      console.log("data inside is ",data) 
      data = data 
     })); 
     console.log("waiting for data",data) 
     yield put({type:"CONTACTS_LOADED",payload:data}); // this is called before the data is fetched causing error 
    } 
    catch (e) { 
      yield put({type: "CONTACTS_LOAD_FAILED", message: e.message}); 
    } 
} 

RPC:

.then((response) => response.json()) 
.then((data)=>{ 
    if (data.error) { 
     if (cb) cb(data.error.message,null); 
    } else { 
     //console.table(data.result); 
     if (cb) cb(null,data.result); 
    } 
}) 

如果我這樣做,將數據返回,但我得到一些例外的產量把

bundle.js:17692 uncaught at rootSaga 
at takeEvery(LOAD_CONTACTS, load_contacts) 
at load_contacts 
TypeError: (0 , _reduxSaga.put) is not a function 
    at load_contacts$ (http://localhost:8088/static/bundle.js:50254:47) 
    at tryCatch (http://localhost:8088/static/bundle.js:93097:40) 
    at Generator.invoke [as _invoke] (http://localhost:8088/static/bundle.js:93335:22) 
    at Generator.prototype.(anonymous function) [as next] (http://localhost:8088/static/bundle.js:93149:21) 
    at next (http://localhost:8088/static/bundle.js:48139:27) 
    at proc (http://localhost:8088/static/bundle.js:48098:3) 
    at runForkEffect (http://localhost:8088/static/bundle.js:48374:19) 
    at runEffect (http://localhost:8088/static/bundle.js:48261:872) 
    at next (http://localhost:8088/static/bundle.js:48143:9) 
    at currCb (http://localhost:8088/static/bundle.js:48215:7 

我刪除了try catch並刪除了用於異常的yield,但我仍然得到了相同的錯誤,似乎問題出在fi上首先收益率。

+0

使用回調裏面'cb'返回數據 –

+0

@ p0k8_我剛剛與測試cb,並且我更新了上面的答案,我仍然對接收數據有任何疑問,有什麼建議嗎? –

回答

0

不知道哪個版本的redux-saga您使用,但putcallfork等現在不同的文件夾

import { put, call, takeEvery } from 'redux-saga/effects'; 
相關問題