admin管理员组文章数量:1399329
I been looking to some code now to simplify for days. Tried many options, but I am not able to figure it out.
I want to make an http request by a function and then get the data back to use in another function. My issue is that I cant seem to retrieve the data in another function. The console.log works fine.
function getData() {
request('http://' + address + ':' + port + '/all.xml', function (err, res) {
if (err) {
console.log('ERROR: Unable to get CyberQ Data')
}
else {
console.log(res.body);
return res.body;
}
});
}
The below code is my original code. Also works like a charm, expect the most important part, res.json. I like to send the data back to the browser, but the result is not available in the function at the place I added res.json.
If either of these two pieces of code can work, I can make my code working. I probably overlook something basic. Thanks in advance for any help
router.get('/bbq/cyberqData', function (req, res) {
request('http://' + address + ':' + port + '/all.xml', function (err, res) {
if (err) {
console.log('ERROR: Unable to get CyberQ Data')
}
else {
console.log('getting the data ');
parseString(res.body, function (err, result) {
console.log(result.nutcallstatus);
return result;
});
}
});
res.json(result);
});
I been looking to some code now to simplify for days. Tried many options, but I am not able to figure it out.
I want to make an http request by a function and then get the data back to use in another function. My issue is that I cant seem to retrieve the data in another function. The console.log works fine.
function getData() {
request('http://' + address + ':' + port + '/all.xml', function (err, res) {
if (err) {
console.log('ERROR: Unable to get CyberQ Data')
}
else {
console.log(res.body);
return res.body;
}
});
}
The below code is my original code. Also works like a charm, expect the most important part, res.json. I like to send the data back to the browser, but the result is not available in the function at the place I added res.json.
If either of these two pieces of code can work, I can make my code working. I probably overlook something basic. Thanks in advance for any help
router.get('/bbq/cyberqData', function (req, res) {
request('http://' + address + ':' + port + '/all.xml', function (err, res) {
if (err) {
console.log('ERROR: Unable to get CyberQ Data')
}
else {
console.log('getting the data ');
parseString(res.body, function (err, result) {
console.log(result.nutcallstatus);
return result;
});
}
});
res.json(result);
});
Share
Improve this question
edited Jun 29, 2015 at 18:37
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jun 29, 2015 at 18:06
DerooieDerooie
1212 silver badges11 bronze badges
3 Answers
Reset to default 3Moving the res.json(result)
in place of return result
should do it.
Note: you may have to rename your variable. You use 2 times res
for 2 different objects
put the res.json(results)
into the callback and fix overlapping of variables.
router.get('/bbq/cyberqData', function (req, res) {
request('http://' + address + ':' + port + '/all.xml', function (err, res1) {
if (err) {
console.log('ERROR: Unable to get CyberQ Data')
}
else {
console.log('getting the data ');
parseString(res1.body, function (err, result) {
console.log(result.nutcallstatus);
res.json(result);
});
}
});
});
You need to put the res.json(results)
into the callback function.
router.get('/bbq/cyberqData', function (req, res) {
request('http://' + address + ':' + port + '/all.xml', function (err, res) {
if (err) {
console.log('ERROR: Unable to get CyberQ Data')
}
else {
console.log('getting the data ');
parseString(res.body, function (err, result) {
console.log(result.nutcallstatus);
res.json(result);
});
}
});
});
You can read more about the asynchronous nature of javascript here.
本文标签: javascriptReturn data from a http request functionStack Overflow
版权声明:本文标题:javascript - Return data from a http request function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744210912a2595412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论