Hi!
I keep getting 404 "Not Found" while i've already set ctx.response.body = token *where toke is a valid token in my tests (with supertest & in postman).
Is there a way to publish the token and override the "Not found" message?
Here is how i'm making the token
const signUp = async (ctx, next) => {
const bodyInfo = await ctx.request.body;
if(!bodyInfo.username || !bodyInfo.password) {
ctx.status = 402;
ctx.body = "Error, username and password must be provided!";
}
const userInst = new User(bodyInfo);
userInst.save(async(err, user) => {
if(err) { return next(err); }
const token = tokenForUser(user);
ctx.body = token; //- <-- Passing the token.
return next();
});
};
export { signUp, signIn };
the route that i hit to get this middleware is
router.post('/signup', signUp);
//- oddly enough
router.post('/signup', signUp, async (ctx, next) => {
const res = await ctx.response;
//- returns a lot of good stuff including `token` but can't publish token outside.
console.log(res);
I wish to override "Not Found" message and the whole ctx.response cause i get really long weird pre made 404s for a route that is preset.
Here is my stack trace.
1) Authentication signs up:
Error: expected 200 "OK", got 404 "Not Found"
at Test._assertStatus (node_modules/supertest/lib/test.js:266:12)
at Test._assertFunction (node_modules/supertest/lib/test.js:281:11)
at Test.assert (node_modules/supertest/lib/test.js:171:18)
at assert (node_modules/supertest/lib/test.js:131:12)
at node_modules/supertest/lib/test.js:128:5
at Test.Request.callback (node_modules/superagent/lib/node/index.js:631:3)
at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:795:18)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Please help and if i'm missing anything i asked this question on stackoverflow here.
Thank you.
It looks like you forgot to put an await before userInst.save, causing the function to return an instantly resolving promise. Does that fix it? If not, you might have a middleware not calling next somewhere before in the chain.
userInst.save(async(err, user) => {
if(err) { return next(err); }
const token = tokenForUser(user);
ctx.body = token; //- <-- Passing the token.
return next();
});
};
this is wrong, use userInst.save like a promise
const user = await userInst.save();
const token = tokenForUser(user);
ctx.body = token;
return next()
if your database manager dont support promises, build your own promise
Wot!?
I had this error for days!
Thank you very much @PlasmaPower & @EduardoRFS !
i also have this error ,how do you fix it
@maotora
@Chen-WeiZhen Hello.
Well it's pretty simple as how @EduardoRFS said.
I was using callbacks while I'm already using async awaits.
async awaits expects a promise so there is no need of callbacks.
Here is the source of what I was creating if it might help. Source
I had this issue and the problem was definitely not waiting for the user to be saved
in my case, it was about calling await next() at the end of the middleware.
Most helpful comment
this is wrong, use userInst.save like a promise
if your database manager dont support promises, build your own promise