CNode

mongoose 不能用ajax获取的数据当做查询条件吗

问答
WWUSO01发布于9 年前最后回复9 年前17 回复5057 浏览0 收藏
    Ques.find({'author': 'admin'})
	  .select('star')
	  .exec((err, stars) => {
		if (err) next(err)
		console.log(stars)
	  });

这样直接写能够获取到author为admin的数据。(ajax的状态是cancel, 但是数据库是操作到了, 把timeout设置长一点, 还是cancel)

但是换做ajax的数据时, 始终不行

	router.post('/star', (req, res) => {
		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)
		  });
		});

不显示数据, 说明是没有找到这个用户(ajax的状态是 failed)

我又这样试了试

	let auth = 'admin'
    Ques.find({'author': auth})
    .select('star')
    .exec((err, stars) => {
      if (err) next(err)
      console.log(stars)
    });

这样也是可以的

ajax请求

	var author = 'XXXX'  // 动态获取的
	$.ajax({
		data: {author: author}, 
		url: ' /star',
		dataType: 'json',
		timeout: 2000,
		type: 'POST',
		success: function(data){
			console.log(data);
		}
	});
	
查看回复

回复 (17)

Z
zhangzeyang#1·9 年前

问题解决了吗?看了半天没发现问题@@

From Noder

W
WUSO01#2·9 年前
引用 zhangzeyang问题解决了吗?看了半天没发现问题@@ From Noder

@zhangzeyang 没解决, 反正从ajax那边获取的数据, 当做查询条件 就不行, 尽管使用前, console出来的 是自己想查询的字符串

Z
zhangzeyang#3·9 年前

debug一下看一看,看是不是异步导致的问题

From Noder

W
WUSO01#4·9 年前
引用 zhangzeyangdebug一下看一看,看是不是异步导致的问题 From Noder

@zhangzeyang 我把ajax的写法也添加到问题里面了

Z
zxj963577494#5·9 年前

VSCode,调试,打一下断点看看

J
jiangli373#6·9 年前

你是不是

router.post('/star', (req, res) => {
		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)
			//todo res.json()
		  });
		});
	```
没有返回响应吧 res.json()这样子的	
W
WUSO01#7·9 年前
引用 jiangli373你是不是 没有返回响应吧 res.json()这样子的

@jiangli373

    router.post('/star', (req, res) => {
		let authors = req.body.author;
		console.log("服务器收到一个Ajax请求,信息为:", authors);
		res.json(['success', "服务器收到一个Ajax 请求,信息为:", authors]);
		});

![MC_QDZML7XF862~PVB2)H0.png

W
WUSO01#8·9 年前
引用 zxj963577494VSCode,调试,打一下断点看看

@zxj963577494 不怎么会调试node,

J
jiangli373#9·9 年前
引用 WUSO01@jiangli373

@WUSO01

router.post('/star', (req, res) => {
		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)
			//todo res.json()
		  });
		});

你这段代码没有返回响应吧

W
WUSO01#10·9 年前
J
jiangli373#11·9 年前
引用 WUSO01@jiangli373 不是很懂

@WUSO01

router.post('/star', (req, res) => {
		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)
			 **`res.json(stars)**`****
		  });
		});

console.log(stars)后面加一个res.json(stars)

W
WUSO01#12·9 年前
引用 jiangli373@WUSO01 console.log(stars)后面加一个res.json(stars)

@jiangli373 报错,Cannot read property 'star' of null

J
jiangli373#13·9 年前
引用 WUSO01@jiangli373 报错,Cannot read property 'star' of null

@WUSO01 是res.json(stars) stars 不是 star

W
WUSO01#14·9 年前
引用 jiangli373@WUSO01 是res.json(stars) stars 不是 star

@jiangli373

    router.post('/star', (req, res) => {
		let authors = req.body.author;  // admin
		Ques.find({'author': authors})
		  .select('star')
		  .exec((err, stars) => {
			if (err) next(err)
			res.json(stars)
		  });
		});

状态200, 返回null

W
WUSO01#15·9 年前
引用 jiangli373@WUSO01 是res.json(stars) stars 不是 star

@jiangli373 router.post('/star', (req, res) => { Ques.find({'author': 'admin'}) .select('star') .exec((err, stars) => { if (err) next(err) res.json(stars) }); }); 写固定的是有返回值

J
jiangli373#16·9 年前
引用 WUSO01@jiangli373 router.post('/star', (req, res) = { Ques.find({'author': 'admin'}) .select('star') ....

@WUSO01 这问题好奇怪 你贴出来的代码看以来应该是没有问题的

B
beyond5959#17·9 年前

你这样一下呢 auth = auth.toString();

参与回复
登录后即可参与回复。登录