2017-06-12 3 views
0

私はredux sagaを試していますが、私のAPIからデータを取得しようとする際に問題があります。 APIを変更してfetchjsを使用していて、何か問題が発生しました。Fetch APIを使用して呼び出し関数にデータを返す方法は?

データが呼び出し元関数に返されていないのに、APIがデータを受信して​​いるという問題があります。私はそれが私がJS(間違った道を)フェッチ使用していますとは何かを持っていると信じて

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); 
    }) 
}; 

/* 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コードです:

は、ここに私の佐賀コードです。私は助けることができる解決策を見つけることができませんでした。コールバックを使用して

更新した場合、

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 

Iを入れてもらいますtry catchを削除し、例外のyieldを削除しましたが、私はまだ同じエラーが出ます最初の利回り。

+0

であるredux-sagaのバージョンを使用するが、その他putcallfork、確かにp0k8_ @データ –

+0

を返すために、コールバック 'cb'を使用私はちょうどでテストcb、私は上記の答えを更新しましたが、私はまだデータ、任意の提案を受信する問題がありますか? –

答えて

0

ない別のフォルダの中に、今

import { put, call, takeEvery } from 'redux-saga/effects'; 
関連する問題