Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

javascript - 无法使用节点JS迭代响应主体和操纵/过滤响应对象(Unable to iterate the response body and manipulate/filter the response object using node JS)

I have below code which is going to invoke REST endpoint and return response back.(我有下面的代码,它将调用REST端点并返回响应。)

I just tried to print the response.body in the console and It works perfectly fine.(我只是试图在控制台中打印response.body,它工作得很好。) var express = require('express'); var app = express(); var PORT = 8001; var request = require('request'); var HashMap = require('hashmap'); var endPoint="http://sandbox.dev.amazon.com/idsearch/environment/amazon/"; app.get('/productId/:proId',async (req,res) => { try { var headers ={ "accept":"application/json" } var options = { url:endPoint.concat(req.params.proId), headers:headers } request(options, (error,response,body)=> { console.log(response.body) // It returned response as below output JSON file res.send("STATUS CODE : 200"); }); } catch(error) { throw error; } }); Output:(输出:) { "<Some dynamic Content>": { "type": "PROD-ID", "environment": "amazon", "tags": [ { "name": "EC-6S0005704A8324S98020", "source": "amazonstage2ma_paymentapiplatserv#TOKEN", "flags": [ "FLAG_DYNAMIC_VALUE", "FLAG_ID_LOOKUP_SUPPORTED" ] } ], "callSummary": [ { "pool": "slingshotrouter", "machine": "stage21007", "apiName": "GET", "status": "0", "duration": 13400.0, "link": "https://www.amazon.qa.pilot.com/Tid-942342192424j2j234" }, { "pool": "slingshot", "machine": "stage21029", "apiName": "GET", "status": "1", "duration": 13368.0, "link": "https://www.amazon.qa.pilot.com/Tid-12342342i842424j2j234" }, { "pool": "devstage_userbridgedomainserv", "machine": "amazon1int-g_userbridgedomainserv_22", "apiName": "POST", "status": "1", "duration": 15.0, "link": "https://www.amazon.qa.pilot.com/Tid-02341723424i842424j2j290" } ], "partial": false } } The above output contains all the responses with respective Endpoint URL which is expected.(上面的输出包含所有带有相应端点URL的响应。) But I just want to fetch only the object contains "Status: 1".(但是我只想获取仅包含“ Status:1”的对象。) I'm just wondering that How can I manipulate the response.body object to get the below JSON as output.(我只是想知道如何处理response.body对象以获取以下JSON作为输出。) Expected Output:(预期产量:) { "callSummary":[ { "pool": "slingshot", "machine": "stage21029", "apiName": "GET", "status": "1", "duration": 13368.0, "link": "https://www.amazon.qa.pilot.com/Tid-12342342i842424j2j234" }, { "pool": "devstage_userbridgedomainserv", "machine": "amazon1int-g_userbridgedomainserv_22", "apiName": "POST", "status": "1", "duration": 15.0, "link": "https://www.amazon.qa.pilot.com/Tid-02341723424i842424j2j290" } ] } I just want to iterate the response.body obj and check the status as 1 if it's then I need to fetch all the details and form it as above payload.(我只想迭代response.body obj并检查状态是否为1,那么我需要获取所有详细信息并将其形成为上述有效负载。) This is dynamic content but the template format is static.(这是动态内容,但是模板格式是静态的。) I tried the below code to iterate the response.body but no luck.(我尝试下面的代码来迭代response.body,但没有运气。) var string = JSON.stringify(response.body); var objectValue = JSON.parse(string); var obj = objectValue.callSummary; console.log(obj.length); // It returned undefined. Please lead me to achieve this.(请带领我实现这一目标。)   ask by ArrchanaMohan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To return that json, just(要返回该json,只需)

res.json(response.body['<Some dynamic Content>'].callSummary); Adding some validation and error handling would be a good idea before sending the response.(在发送响应之前,添加一些验证和错误处理将是一个好主意。) In case your key is just an example and you never know your first key value, and you always want the values of the first key(如果您的键只是一个例子,而您却不知道第一个键的值, 您总是想要第一个键的值) res.json(Object.values(response.body)[0].callSummary); Object.values returns an array, so you can iterate the values if you want manage more than the first one(Object.values返回一个数组,因此,如果要管理多个值,则可以迭代这些值)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...