search

Home  >  Q&A  >  body text

javascript - Can mongoose not use the obtained ajax data as query conditions?

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);
        }
      });
漂亮男人漂亮男人2739 days ago651

reply all(1)I'll reply

  • 阿神

    阿神2017-05-17 10:04:33

    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!

    reply
    0
  • Cancelreply