M
M
matveyvarg2015-10-31 15:10:14
JavaScript
matveyvarg, 2015-10-31 15:10:14

Why doesn't the res.send function work?

app.get('/watch',function(req, res) {
 	var id = req.query.rid,
 	      code = req.query.codes;
  	getTokenFromVK(code,function(acc){
 		console.log("access_token : "+acc);
 		access_token = acc;
 		console.log('resc '+res);
 		if(req.query.btn=="create"){
 			roomid = createRoom('Test');
 			console.log("roomid="+roomid);
 			res.send('/watch/'+roomid);
 		} 
 		else if (req.query.btn=="join") {
 			res.send('/watch/'+id);
 		}
 	});
});

Why doesn't res.send work? If you remove this block
if(req.query.btn=="create"){
 			roomid = createRoom('Test');
 			console.log("roomid="+roomid);
 			res.send('/watch/'+roomid);
 		} 
 		else if (req.query.btn=="join") {
 			res.send('/watch/'+id);
 		}
for the callback function, then everything works fine. What could be the problem ?
PS the getTokenFromVK function itself:
function getTokenFromVK(codes, callback) {
  request.get('https://oauth.vk.com/access_token?client_id=5124623&client_secret=ETFO5R3J8xgnu4CjpKt5&redirect_uri=localhost:3000/home&code='+codes,function (err,res,body){
    _access_token = body.split(":")[1].split(",")[0].split("\"")[1];
    callback(access_token);
  });
  
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Markus Kelvin, 2015-11-01
@mmxdesign

The point here is not res.send, but the fact that inside callback - req.query.btn is invisible (that is, scope does not allow it to be there) therefore none of your if(req.query.btn=="create") or else if (req.query.btn=="join") doesn't work... an
easy way to assign req.query.btn to var btnCheck and therefore check it inside the callback...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question