为什么我的sailsjs服务会返回"undefined"?到调用控制器的动作? [英] Why does my sailsjs service return "undefined" to the calling controller action?
问题描述
我必须发布到其他人开发的API才能获得授权代码,并且由于必须在几个不同的上下文中这样做,我认为我应该将代码移到处理POST并获取响应的地方服务.
I have to POST to an API that someone else has developed in order to obtain an authorization code, and as I have to do it in several different contexts I thought I'd move the code for handling the POST and getting the response to a service.
此刻令人沮丧的是,我似乎正在从API中获得想要的值,但无法将其从服务器返回给调用的sails控制器.
The frustrating thing at the moment is that I seem to be getting back the value that I want from the API, but can't return it from the server to the calling sails controller.
这是服务源代码:
module.exports = {
getVerifyCode: function(uuid, ip_addr) {
console.log (uuid);
console.log (ip_addr);
var http = require('http'),
querystring = require('querystring'),
// data to send
send_data = querystring.stringify({
uuid : uuid,
ip_addr : ip_addr
}),
// options for posting to api
options = {
host: sails.config.api.host,
port: sails.config.api.port,
path: '/verifyCode/update',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(send_data)
}
},
json_output = "",
// post request
post_req = http.request(options, function(post_res) {
post_res.on('data', function(chunk) {
json_output += chunk;
});
post_res.on('end', function() {
var json_data = JSON.parse(json_output),
verify_code = json_data.verify_code;
console.log("code="+verify_code);
return ('vc='+verify_code);
});
});
post_req.write(send_data);
post_req.end();
}
}
这是我的控制器操作中的两行相关内容:
And here's two relevant lines from my controller action:
var vc = verify.getVerifyCode(req.session.uuid, req.connection.remoteAddress);
console.log('vc='+vc);
奇怪的是,控制器控制台日志是在服务执行之前写入的:
The weird thing is that the controller console log gets written before the service one does:
vc=undefined
code=YoAr3ofWRIFGpoi4cRRehP3eH+MHYo3EogvDNcrNDTc=
有什么想法吗?我运行的服务要简单得多(只是一些字符串操作的东西);我觉得这里的问题与API请求和响应的异步性质有关.
Any ideas? I have a much simpler service running (just some string manipulation stuff); I have a feeling the issue here relates to the asynchronous nature of the API request and response.
推荐答案
Jasper,您的正确假设是"API请求和响应的异步性质".
Jasper, your correct in your assumption that it is the " asynchronous nature of the API request and response".
当您在 verify
服务中执行http调用时,节点会进行该调用,然后它们继续处理其余代码 console.log('vc ='+ vc);.
,并且不等待http调用结束.
When you execute your http call in your verify
service, node makes that call and them moves on to the rest of the code console.log('vc='+vc);
and does not wait for the http call to finish.
我不确定最终结果应该是什么,但是您可以重写控制器/服务以包含回调(这只是一个选项,当然有很多方法可以做到这一点,其他人应该建议其他人)
I'm not sure what your end result should be but you can rewrite your controller / service to include the callback (this is just one options, there are many ways to do this of course, other people should suggest others)
verify.js
verify.js
getVerifyCode: function(uuid, ip_addr, cb) {
// Bunch of stuff
return post_req = http.request(options, cb(post_res});
}
然后在您的控制器中controller.js
then in your controller controller.js
verify.getVerifyCode(req.session.uuid, req.connection.remoteAddress, function(resps){
// code in here to execute when http call is finished
})
这篇关于为什么我的sailsjs服务会返回"undefined"?到调用控制器的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!