提取然后返回_bodyBlob _bodyInit

时间:2019-02-24 11:44:17

标签: node.js reactjs fetch fetch-api

当我像在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),返回数据,返回数据。 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

1 个答案:

答案 0 :(得分:1)

我只是这样编码前端

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