javascript - Can mongoose not use the obtained ajax data as query conditions?
漂亮男人
漂亮男人 2017-05-17 10:02:33
0
1
665
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);
        }
      });
漂亮男人
漂亮男人

reply all(1)
阿神

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!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template