[英]Only part of the response is retrieved at the ionic 4 front-end
I am sending an http GET request from ionic to a NodeJS server using tokens. 我正在使用令牌将来自ionic的http GET请求发送到NodeJS服务器。 I get a message that data has been successfully retrieved.
我收到一条消息,提示已成功检索数据。 However, the data property of the response is not sent to the frontend and is assigned to undefined when i try to log it in the console.
但是,当我尝试将其记录在控制台中时,响应的数据属性不会发送到前端,而是分配给undefined。 Here is the code for my ionic request:
这是我的离子请求的代码:
getPoints() {
var headers = new Headers();
headers.append("Authorization", "Bearer " + this.getAuthorizationToken());
headers.append("Content-Type", "application/json");
console.log(this.getAuthorizationToken());
return this.http
.get(environment.apiUrl + "/user/getCurrentPoints", {
headers
})
.map(res => res.json());
}
This is the code inside an ionic component where I am calling the GET method in the constructor: 这是离子组件内部的代码,我在构造函数中调用GET方法:
constructor(
ease: RoundProgressEase,
private _router: Router,
private formB: FormBuilder,
private _authService: AuthService
) {
for (let prop in ease) {
if (prop.toLowerCase().indexOf("ease") > -1) {
this.animations.push(prop);
}
}
this._authService.getPoints().subscribe((res: any) => {
console.log("data points", res);
});
}
Also here is the endpoint at the backend: 这也是后端的端点:
router.get(
"/user/getCurrentPoints",
isAuthenticated,
userCtrl.getCurrentPoints
);
Here is the middleware isAuthenticated: 这是经过身份验证的中间件:
var isAuthenticated = function(req, res, next) {
var token = req.headers["authorization"];
console.log(req.header);
token = req.headers.authorization.split("Bearer ")[1];
if (!token) {
return res.status(401).json({
error: null,
msg: "You have to login first before you can access your lists.",
data: null
});
}
jwt.verify(token, req.app.get("secret"), function(err, decodedToken) {
if (err) {
return res.status(401).json({
error: err,
msg: "Login timed out, please login again.",
data: null
});
}
req.decodedToken = decodedToken;
console.log(req.decodedToken);
next();
});
};
Finally, this is the userCtrl.getCurrentPoints
controller method: 最后,这是
userCtrl.getCurrentPoints
控制器方法:
module.exports.getCurrentPoints = function(req, res, next) {
console.log("accessed get current points");
if (!Validations.isString(req.decodedToken.user.username)) {
return res.status(422).json({
err: null,
msg: "type parameter must be a valid String.",
data: null
});
}
User.find({ username: req.decodedToken.user.username }).exec(function(
err,
user
) {
if (err) {
return next(err);
}
res.status(200).json({
err: null,
msg: "Current user points retrieved successfully.",
data: user.points
});
});
};
This is the response i get 这是我得到的回应
{err: null, msg: "Current user points retrieved successfully."}
The user schema 用户架构
const mongoose = require('mongoose');
var Voucher = mongoose.Schema.Types.Voucher;
const userSchema = mongoose.Schema({
username: {
type: String,
required: true,
trim: true,
lowercase: true
},
password: {
type: String,
required: true,
trim: true
},
name: {
type: String,
required: true,
trim: true
},
region: {
type: String,
required: true,
trim: true,
},
building: {
type: Number,
required: true,
min: 0
},
points: {
type: Number,
required: false,
min: 0
},
vouchers: {
type: [Voucher],
status:[String],
required: false,
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: Date
});
mongoose.model('User', userSchema);
What you get from backend? 您从后端得到什么? err or data?
错误或数据? First check that your request gets the backend api.
首先检查您的请求是否获取后端api。 Second I saw that you checked for lower-case authorization in middle ware;
其次,我看到您检查了中间件中的小写授权。 maybe that's where things go wrong.
也许那是出问题的地方。 Third maybe the user has no points at all and by default the null field is eliminated from the response.
第三,用户根本没有任何点,默认情况下,从响应中删除空字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.