javascript - mongoose 不能用获取的ajax数据当做查询条件吗
问题描述
Ques.find({’author’: ’admin’}) .select(’star’) .exec((err, stars) => { if (err) next(err) console.log(stars) });
这样直接写能够获取到author为admin的数据。
但是换做ajax的数据时, 始终不行
let authors = req.body.author; console.log('服务器收到一个Ajax请求,信息为:', authors); console.log(typeof(authors)) // string let auth = authors console.log(auth) // admin Ques.find({’author’: auth}) .select(’star’) .exec((err, stars) => { if (err) next(err) console.log(stars) });
不显示数据, 说明是没有找到这个用户
我又这样试了试
let auth = ’admin’ Ques.find({’author’: auth}) .select(’star’) .exec((err, stars) => { if (err) next(err) console.log(stars) });
这样也是可以的
ajax请求
let author = XXX; // 动态获取的 $.ajax({data: {author: author},url: ’/star’,dataType: ’json’,timeout: 2000,type: 'POST',success: function(data){ console.log(data);} });
问题解答
回答1:供参考。因为是AJAX调用过来的,把结果返回到调用的地方显示,而不是console打印。
Love MongoDB! Have Fun!
相关文章:
1. docker绑定了nginx端口 外部访问不到2. python - 使用pandas的resample报错3. 网页爬虫 - python 爬取网站 并解析非json内容4. python - flask post提交timestamp不能作为参数,这是为什么?5. android - 类似于微信朋友圈图片这样的是在listview中嵌套gridview还是动态加入多个imageview呢?6. docker-machine添加一个已有的docker主机问题7. html - 类似这样的弹层用什么插件写比较好?8. vue.js - vue-router开启HTML5的history模式后nginx配置9. html5 - 图片一般一怎么的形式存放在服务器中的?10. node.js - mongodb查找子对象的名称为某个值的对象的方法

网公网安备