Tuesday, 18 April 2017

MongoDB search button Ajax request

I have a search button that onkeydown it submits a form that request an ajax request and when that happens i dump the result matching with the value that's in input. But the problem is I can't chain it back to HTML Dom. I have tried res.locals it didn't work? help

app.post('/ajax', function(req, res){

  Universite.find({}, function(err, college){
    var myCollegeNames = [];
    college.forEach(function(item, index){
    myCollegeNames.push(item.schoolName);;
    });
    var result = [];
    for(var i = 0; i< myCollegeNames.length; i++){
    if(myCollegeNames[i].match(/[req.body.search]/gi)){
      result.push(myCollegeNames[i]);
      }
    }
    console.log(result);
    res.locals.result = result;
    res.send('');
    });

    return false;
});

This is the ajax request that i linked to my page

$(document).ready(function(){
  $('form').on('submit', function(){
    var item = $('form input');
    var itemVal = item.val();
    $.ajax({
      type: 'POST',
      url: '/ajax',
      data: itemVal,
      success: function(){
        for(var i = 0; i< 5; i++){
          console.log('o');
        }
        $('ul').append('<li>OKe</li>');
      }
    })
    return false;
  });
});



via Can Unbay

No comments:

Post a Comment