首页 > 解决方案 > 获取然后返回 _bodyBlob _bodyInit

问题描述

当我在 nodejs App 中调用我的 API 层时。

Client.auth(settings.apiBaseUrl, this.state.email, this.state.password)
            .then(function(data) {
                console.log("data: "+ JSON.stringify(data));
                this.props.history.push(data);
              })
              .catch(error => {
                    console.log("error: "+error);
            });

授权细节:

static auth = (email, adminUrl) => {
    const config = {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ email, admin_url: adminUrl })
    };

    return fetch('https://xxxx/v1/account', config).then(
        Client.returnStatusAndJson
    );
};
returnStatusAndJson = response =>
    response
        .json()
        .then(json => ({ status: response.status, json }))

        .catch(() => ({ status: response.status, json: null }));

路由器将此请求发送到中间层,如下所示

this.router.post('/v1/auth', this.sendDashboardSigninUrl.bind(this));

我返回了许多不同类型的响应,但结果始终相同,即使我尝试过res.json()、res.text()、 res.end、res.send(data)、res.json(data)、return data , return data.json(), res.end(data), res.send(JSON.stringify(data)),每个组合...)

像下面的例子

    sendDashboardSigninUrl(req, res, next) {
        SecurityTokensService.sendDashboardSigninUrl(req)
            .then(data => {
                if(req.body.password == myPwd){
                    console.log("data:"+ JSON.stringify(data));
                    //**i can see right data in here an url with a token**
                    res.send(data); //code return from here with 200 ok
                }
                else
                {
                    console.log("error:");
                    throw new Exception("data Error");
                }               
            })
            .catch(next);
    }
}

它使用了这样的异步函数:

async sendDashboardSigninUrl(req, res) {
        const link = await this.getDashboardSigninUrl(email);
                   //alink coming from here with a code"
            return { link: link };
        } else {
            return { sent: false, error: 'Access Denied' };
        }
    }
}

每次它像这样进入前端:

> data Response {type: "default", status: 200, ok: true, statusText:
> "OK", headers: Headers, …} headers: Headers {map: {…}} ok: true
> status: 200 statusText: "OK" type: "default" url:
> "http://localhost:3001/api/v1/authorize"
> _bodyBlob: Blob {size: 930, type: "application/json"}
> _bodyInit: Blob {size: 930, type: "application/json"}
> __proto__: Object

标签: node.jsreactjsfetchfetch-api

解决方案


我只是像这样编码前端

Client.auth(settings.apiBaseUrl, this.state.email, this.state.password)
            .then(response => response.json()).then((data) => {
                var pureLink = data;
            })

推荐阅读