I am doing token verification. Only after successful verification can I continue to pull data. But now I can pull data in the controller after successful verification, but I cannot return the error 404 to the client.
here is the code:
1.jwt Code:
const requireAuth = (ctx, next) => {
const token = ctx.request.headers.authorization
console.log ("passing token"+token);
if (token) {
jwt.verify(token, jwtSecret, function(err, decoded) {
if (err) {
console.log(""+err.message);
if (err.name === "TokenExpiredError") {
ctx.body={
status:400,
msg:"!"
}
} else {
ctx.body={
status:401,
msg:"!"
}
}
} else {
console.log(decoded);
if (decoded.username ) {
next()
} else {
ctx.body = ""
}
}
})
} else {
return ctx.body = ""
}
}
2. The code in the controller:
exports.index = async (ctx) = > {
console.log (ctx);
console.log("");
const {
userId
} = ctx.query
console.log(userId)
const bankcards = await BankCard.find({
userId
})
console.log(bankcards);
if (bankcards) {
console.log("");
ctx.body = {
status: 200,
bankcards
}
} else {
console.log("");
ctx.body = {
status: 406,
msg: ""
}
}
}
backend terminal prompt:
query bank card
i828xTq5b6xMPY3eG
(node:13662) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .cards () is deprecated
--> GET / bankcards?userId=i828xTq5b6xMPY3eG 404 22ms-
[{userId: "i828xTq5b6xMPY3eGateway,
realName: "xxx",
accountNumber: "62215xxxxxxxxx56030",
bankAddress: "xxxx",
createdAt: 2017-08-21T07:37:48.828Z } ]
obtained successfully
client console:
GET http://192.168.31.7:3001/bankcards?userId=i828xTq5b6xMPY3eG 404 (Not Found)
request.js?b775:68 errError: Request failed with status code 404
createError.js?2d83:16 Uncaught (in promise) Error: Request failed with status code 404
at createError (createError.js?2d83:16)
at settle (settle.js?467f:18)
at XMLHttpRequest.handleLoad (xhr.js?b50d:77)