node.js - 获取然后返回 _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
解决方案
我只是像这样编码前端
Client.auth(settings.apiBaseUrl, this.state.email, this.state.password)
.then(response => response.json()).then((data) => {
var pureLink = data;
})
推荐阅读
- android - 在 Jetpack Compose 中截取 Webview (AndroidView)
- python - 如何返回格式字符串以及为什么我的按钮不显示?
- php - 如何使用相同的headears和params发送两个带有curl的请求
- javascript - 在 react CRUD 操作中遇到一些错误?
- java - URI removes / from path
- c# - NUnit Test 命令首先在基类中运行测试
- plugins - Gazebo sensor plugins for barometric pressure Sensor with geometry_msgs/PointStamped?
- kubernetes - How to install Istio ingress,egress and other components as DeamonSet in Cluster
- python - TypeError: 'str' object is not callable while give title to the matplotlib.pyplot of line line plot
- c# - System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'Transaction'