所以当我尝试类似
route.post('/signup',async (ctx)=>{
try{
const hashed = await bcrypt.hash(ctx.request.body.password,10);
let newUser = await assignUser(ctx.request.body,hashed).catch(error=>{throw Error(error)});
console.log(newUser);
}
catch(error){
console.log(error.message);
ctx.status = 500;
return ctx.body={success:false,message:error.message};
}
return ctx.body = {success:true,message:'An email has been sent to your address click link to verify your address'};
});
当我尝试使用此代码并将请求发送到api时,它将在Postman中返回404状态未找到。我期望的输出是错误消息或
{success:true,message:'An email has been sent to your address click link to verify your address'}
作为邮递员中的json。当我在控制台中登录newUser时,它会显示哈希密码和用户对象,并且当出现错误时,将记录错误消息,但是从请求中返回的任何内容都不会返回,只是找不到404。
这里是assignUser
代码
function assignUser(body,hashed){
return new Promise((resolve,reject)=>{
let newUser = new User;
newUser.name = body.name;
newUser.lastName = body.lastName;
newUser.email = body.email;
newUser.age = body.age;
newUser.username = body.username;
newUser.password = hashed;
resolve(newUser);
});
}
和依赖项
"dependencies": {
"bcrypt": "^3.0.0",
"jsonwebtoken": "^8.3.0",
"koa": "^2.5.2",
"koa-body": "^4.0.4",
"koa-router": "^7.4.0",
"mongoose": "^5.2.12"
}