0

我用 koa 和 koa-router 设置了一个 REST api。我可以达到一个 api 端点,console.log(ctx.body)并且我看到了我想要和期望看到的东西。在这种情况下,我希望看到一个带有 URL 的 json 对象,这就是我在控制它时在节点端看到的:

etrade_api: { oauth_token: 'hidden',
oauth_token_secret: 'hidden',
authorizeUrl: 'https://us.etrade.com/e/t/etws/authorize?key=hidden&token=hidden' }

但是,当我尝试在 React 中使用端点时,我在客户端上得到的响应是一个 Response 对象,如下所示:

Response {url: "http://localhost:3000/api/verification", status: 200, statusText: "OK", headers: Headers, ok: true…}
_abort:false
_raw:Array[0]
body:PassThrough
bodyUsed:false
headers:Headers
ok:true
size:0
status:200
statusText:"OK"
timeout:0
url:"http://localhost:3000/api/verification"
__proto__:Body

我的问题是如何在客户端上面的节点 console.log 中获取我期望的 json 对象?

我对 koa 很陌生,并且一直在尝试通过学习其他人的示例来了解他们如何设置 rest api。目前我的服务器如下所示:

import Koa from 'koa';
import convert from 'koa-convert';
import historyApiFallback from 'koa-connect-history-api-fallback';
import serve from 'koa-static';
import body from 'koa-better-body';
import error from 'koa-error';
import compress from 'koa-compress';
import session from 'koa-session';
import responseTime from 'koa-response-time';
import logger from 'koa-logger';
import config from '../settings/config';
import routes from './api/router/routes';


const paths = config.utils_paths;
const app = new Koa();

app.keys = ['somethin up in here'];

app.use(responseTime());
app.use(error());
app.use(logger());
app.use(convert(session(app)));

// Setup api routes
app.use(body());
routes(app);

// This rewrites all routes requests to the root /index.html file
// (ignoring file requests). If you want to implement isomorphic
// rendering, you'll want to remove this middleware.
app.use(convert(historyApiFallback({
    verbose: false
})));

app.use(convert(serve(paths.client('static'))));
app.use(compress());
app.listen(3000);

我的路线文件如下所示:

import Router from 'koa-router';
import account_routes from '../accounts'; //import controllers

export default function (app) {
    const router = new Router({
        prefix: '/api'
    });

    account_routes(router);

    app.use(router.routes());
    app.use(router.allowedMethods());
}

最后我的控制器如下所示:

import etApi from '../etrade_api';

export default function(router){
    router.get('/verification', getEtradeVerificationLink);
    // other routes here
}

async function getEtradeVerificationLink( ctx, next ) {
    const myKey = 'hidden';
    const mySecret = 'hidden';

    try {
        ctx.body = await etApi.requestToken(myKey, mySecret);
        console.log('etrade_api:', ctx.body); // this prints out what I expect to see
    }
    catch ( error ) {
        console.error('Failed to get verification link.', error);
    }
}

感谢您查看此内容并提供任何帮助。

4

1 回答 1

0

@pe8ter 让我走上了正确的道路。原来我的服务器端代码没有问题,这是我的客户端代码。我正在使用 fetch 来使用 api,但我没有处理 fetch 正确返回的承诺。我需要做的就是改变我的客户端代码:

export function fetchVerificationLink() {
    return async( dispatch ) => {
        dispatch(requestVerificationLink());

        let response, link;
        try {
            response = await read(urls.etradeVerification); // I have a wrapper around node-fetch, but failed to handle the promise that fetch returns

            dispatch(receiveVerificationLink(response));
        }
        catch ( error ) {
            console.error('Unable to get verification link from eTrade.', error);
        }
    }
}

我将代码更改为此以处理来自 fetch 的响应:

export function fetchVerificationLink() {
    return async( dispatch ) => {
        dispatch(requestVerificationLink());

        let response, link;
        try {
            response = await read(urls.etradeVerification);
            try {
                link = await response.json();  // Here was the solution to my problem.
                dispatch(receiveVerificationLink(link));
            }
            catch ( error ) {
                console.error('Unable to parse authorization link response.', error);
            }                
        }
        catch ( error ) {
            console.error('Unable to get verification link from eTrade.', error);
        }
    }
}

现在一切都按预期工作。感谢您的协助!

于 2016-04-25T02:51:16.397 回答