为了账号安全,请及时绑定邮箱和手机立即绑定

koa处理xhr请求,服务端能接受并顺利处理,但chorme显示xhr.status=404

koa处理xhr请求,服务端能接受并顺利处理,但chorme显示xhr.status=404

扬帆大鱼 2019-02-25 04:27:08
xhr请求,koa处理,服务端能接受并顺利处理,但无法返回response,xhr.status=404,但将路由里的代码从向MongoDB获取数据 换成 简单的赋值返回,则一切正常,xhr.status=200.如下图,如果换成注释内的代码,则正常执行请问是为什么?以及解决方法。谢谢你们啦~~
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

userath()这个函数是没有返回值的

响应的话 ctx.body=response


mongoose支持promise,配合await使用


try{

    var user = awiat userModel.find({name: ctx.request.body.username});

    if(user.length == 0){

        ctx.body = "user not register"

    } else {

        let user = await userModel.find({

            name: ctx.request.body.username,

            password: ctx.request.body.password

            });

        if (user == null || user.length == 0) {

            console.log(user);

            console.log('password wrong');

            ctx.body = "login fail";

         

        }

        if (user != null && user.length > 0) {

            console.log('login sccessful');

            ctx.body = "login successful";

        }

    }

} catch(e){

}


查看完整回答
反对 回复 2019-03-02
  • 1 回答
  • 0 关注
  • 893 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号