Ques.find({'author': 'admin'})
.select('star')
.exec((err, stars) => {
if (err) next(err)
console.log(stars)
});
Writing directly in this way can obtain the data whose author is admin.
But when changing to ajax data, it never works
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)
});
No data is displayed, indicating that the user was not found
I tried this again
let auth = 'admin'
Ques.find({'author': auth})
.select('star')
.exec((err, stars) => {
if (err) next(err)
console.log(stars)
});
This is also possible
ajax request
let author = XXX; // 动态获取的
$.ajax({
data: {author: author},
url: '/star',
dataType: 'json',
timeout: 2000,
type: "POST",
success: function(data){
console.log(data);
}
});
For reference. Because it is called by AJAX, the results are returned to the calling place for display instead of console printing.
Love MongoDB! Have Fun!