What am I doing wrong in my code here since I can't get the 'qkey' value?
If I test '/search/firstname/daniel' in my browser, my response is: {"qkey":"daniel"}
app.get('/search/:qkey/:qvalue', function(req, res){
var qkey = req.params.qkey;
var qvalue = req.params.qvalue;
var query = {qkey:qvalue}
console.log(query);
res.send(query);
});
The problem is the way you create query
object. You can't assign a dynamic value to a property name using curly bracket syntax. What you should do instead is;
var query = {};
query[qkey] = qvalue;
you can send data to view as follow:
//in the server side ...
app.get('/search/:qkey/:qvalue', function(req, res){
res.write(JSON.stringify({
qkey:req.params.qkey;
qvalue:req.params.qvalue;
}));
});
and in the client side... call to ajax
$.ajax({
type:"POST",
url:"/search/"+qkey+"/"+qvalue,
success: function(data){
var string = eval("(" + data + ")");
//you access to server response with
console.log(string.qkey+" and "+ string.qvalue);
}
});