problem description
in egg, I got the openid, through the user"s code request Wechat API. I found that I got it the first time, but it was executed twice, so ctx.body directly printed the second result, and code has been used. How to modify the request only once?
the environmental background of the problems and what methods you have tried
I have tried to use ctx.throw ("500", "xxxxx") to throw an error, which can solve the printing problem (but it has been requested twice), and it cannot set 200
related codes
class User extends Controller {
async login() {
const ctx = this.ctx;
const result = await ctx.curl(
`https://api.weixin.qq.com/sns/jscode2session?appid=${
this.app.config.appid
}&secret=${this.app.config.secret}&js_code=${
ctx.query.code
}&grant_type=authorization_code`,
{
method: "GET",
dataType: "json",
headers: this.app.config.headers
}
)
console.log(result.data)
const openid = result.data.openid
if (openid) {
ctx.body = {
openid
}
} else {
ctx.throw(500, "openid");
}
}
}
what result do you expect? What is the error message actually seen?
request only once.