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

xhr请求,koa处理,服务端能接受并顺利处理,但无法返回response,xhr.status=404,但将路由里的代码从向MongoDB获取数据 换成 简单的赋值返回,则一切正常,xhr.status=200.如下图,如果换成注释内的代码,则正常执行

https://img.mukewang.com/5c7a1a6b0001dedc05280919.jpg

请问是为什么?以及解决方法。谢谢你们啦~~

扬帆大鱼
浏览 894回答 1
1回答

POPMUISE

userath()这个函数是没有返回值的响应的话 ctx.body=responsemongoose支持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){}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript