[英]How to cast/parse API response from Node.JS
I have a line of code in Node.js that call an external API: 我在Node.js中有一行代码可以调用外部API:
public.getInfo(console.log, config.data1);
and retrieve some data: 并检索一些数据:
null { abc:
{field1: 0.234252,
{field2: 0.234252,
{field3: 0.234252,
{field4: 1,
{field5: 0.234252 },
{ xzy:
{field1: 0.234252,
{field2: 0.234252,
{field3: 0.234252,
{field4: 0.234252,
{field5: 0.234252 }}
I can see the result in the console. 我可以在控制台中看到结果。 I have two questions:
我有两个问题:
public.getInfo(console.log, config.data1);
has console.log as "callback" function. 具有console.log作为“回调”功能。 What if I do not want to print this data, but just use it in other functions in my code?
如果我不想打印此数据,而只在代码中的其他函数中使用该怎么办?
How do I "read" the API response? 如何“读取” API响应? How do I use the JSON structure of the response?
如何使用响应的JSON结构? For example:
abc -> fiels4 => 1
例如:
abc -> fiels4 => 1
I'm not sure due to lack of code, but I think config
is a variable you have set somewhere and you receive data from config.data1
. 由于缺乏代码,我不确定,但是我认为
config
是您在某个地方设置的变量,并且您从config.data1
接收数据。
Use config.data1[0].field4
to read the object property of abc -> field4 => 1 使用
config.data1[0].field4
读取abc的对象属性-> field4 => 1
Or read the other ways to read properties https://www.w3schools.com/js/js_properties.asp 或阅读其他读取属性的方法https://www.w3schools.com/js/js_properties.asp
I have actually founded a solution... 我实际上已经找到了解决方案...
public.getInfo(function(err,data){
console.log(data[config.data1].field4)
return true
},
config.data1);
This gives me the correct value of the returned API call... (in the console)... 这为我提供了返回的API调用的正确值...(在控制台中)...
But how to use the value outside the public.getInfo
function? 但是如何在
public.getInfo
函数之外使用该值? do I need to create a variable? 我需要创建一个变量吗? confused
困惑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.