var keystone = require('keystone');

exports = module.exports = function(req, res) {

	var query = req.body.query;
	var cookie = req.cookies.cookieCountry;

	// https://docs.mongodb.com/manual/reference/operator/query/regex/
	var q = keystone.list("Country").model.find({'name': {$regex: '.*' + query + '.*', $options: 'i'}});

	q.exec(function(err, result){
		if(result == ''){
		console.log('The country you have searched doesn not exist in our database yet...')
		}
		else{
		var result = JSON.stringify(result);
		result = result.substring(1,result.length);
		result = result.substring(0,result.length-1);
		county = JSON.parse(result);

		//extract the 'name' from results
		//var countryName = result.name; or result['name'];-> doesn't work
		//console.log(countryName);

		//https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client
		res.cookie('country', county._id).redirect("/");
		}
	});
}
@@ -88,69 +88,66 @@
 var 
-query = req.body.query;%0A%09var cookie = req.cookies.cookieCount
+view = new keystone.View(req, res),%0A%09%09query = req.body.que
 ry;%0A
@@ -349,16 +349,38 @@
 esult)%7B%0A
+%09%09flashErrors: true;%0A%0A
 %09%09if(res
@@ -388,16 +388,23 @@
 lt == ''
+ %7C%7C err
 )%7B%0A%09%09con
@@ -483,16 +483,157 @@
 yet...')
+;%0A%09%09req.flash('error', 'The country you have searched doesn not exist in our database yet...');%0A%09%09view.render(keystone.lang + '/errors/404');
 %0A%09%09%7D%0A%09%09e
@@ -777,16 +777,17 @@
 %0A%09%09count
+r
 y = JSON
@@ -807,144 +807,8 @@
 );%0A%0A
-%09%09//extract the 'name' from results%0A%09%09//var countryName = result.name; or result%5B'name'%5D;-%3E doesn't work%0A%09%09//console.log(countryName);%0A%0A
 %09%09//
@@ -937,16 +937,17 @@
 ', count
+r
 y._id).r

Handle Error in Search form with a flash message and 404 view