javascript - How do I access this property of an object from a RESTful API? -
i'm writing community today i'm having confusion while trying access object provided api.
api used:
- cloudflare's v4 api used: applicable documentation available @ https://api.cloudflare.com/#user-level-firewall-access-rule-list-access-rules
concept:
i'll receive object identical myobject when accessing api make sure understand object i've included source code can access below.
the object pretty formating
{ "result": [{ "id": "e9a821b6379bd22539226a4cc6956144", "mode": "challenge", "allowed_modes": ["block", "challenge", "whitelist", "js_challenge"], "status": "active", "notes": "by api", "scope": { "id": "981969ce21909a61b62905c585ec9aaa", "email": "user@example.com", "type": "user" }, "configuration": { "value": "ad", "target": "country" }, "created_on": "2017-08-06t17:05:19.126546z", "modified_on": "2017-08-06t17:05:19.126546z" }], "result_info": { "page": 1, "per_page": 1, "total_pages": 251, "count": 1, "total_count": 251 }, "success": true, "errors": [], "messages": [] }
the code below returns number 251 without issue what's expected.
var myobject = {"result":[{"id":"e9a821b6379bd22539226a4cc6956144","mode":"challenge","allowed_modes":["block","challenge","whitelist","js_challenge"],"status":"active","notes":"by api","scope":{"id":"981969ce21909a61b62905c585ec9aaa","email":"user@example.com","type":"user"},"configuration":{"value":"ad","target":"country"},"created_on":"2017-08-06t17:05:19.126546z","modified_on":"2017-08-06t17:05:19.126546z"}],"result_info":{"page":1,"per_page":1,"total_pages":251,"count":1,"total_count":251},"success":true,"errors":[],"messages":[]} console.log(myobject["result_info"]["total_pages"])
in real use:
this script ran in node, configure module contains api keys , stuff (they shouldn't exposed obvious reasons).
// import required modules var request = require('request'); var config = require('./config'); // setup account details , site details (to edit these variables edit config.js) var accountemail = config.accountemail; var globalapikey = config.globalapikey; // setup other needed variables var userfirewallruleids = []; // store ids of each firewall rule in array var currentpage = 1; var pagecount = 0; // page count (doing first request twice less efficient - refactor in future) // configure headers (pretend curl now) var headers = { 'user-agent': 'curl/7.47.0', 'content-type': 'application/json', 'x-auth-email': accountemail, 'x-auth-key': globalapikey, } // configure request var options = { url: 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules?mode=challenge&configuration_target=country&page=1&per_page=1&order=mode&direction=desc&match=all', method: 'get', headers: headers } // request (in production can remove console.log lines or comment out - default it'll return entire response or bad can see went or wrong) request(options, function(error, response, body) { if (!error && response.statuscode == 200) { console.log(body["result_info"]["total_pages"]); } else { console.log(body); } })
however unlike concept code running console.log(body["result_info"]["total_pages"]);
return error.
/documents/coding/cloudflare-scripts/remove-user-level-firewall-rules.js:44 console.log(body["result_info"]["total_pages"]); ^ typeerror: cannot read property 'total_pages' of undefined @ request._callback (/users/nathanielsuchy/documents/coding/cloudflare-scripts/remove-user-level-firewall-rules.js:44:36) @ request.self.callback (/users/nathanielsuchy/node_modules/request/request.js:188:22) @ emittwo (events.js:125:13) @ request.emit (events.js:213:7) @ request.<anonymous> (/users/nathanielsuchy/node_modules/request/request.js:1171:10) @ emitone (events.js:115:13) @ request.emit (events.js:210:7) @ incomingmessage.<anonymous> (/users/nathanielsuchy/node_modules/request/request.js:1091:12) @ object.oncewrapper (events.js:314:30) @ emitnone (events.js:110:20)
how should go fixing script can access page count directly api's object output?
after digging issue further , other answers , comments able find solution.
while example code json parse bit off , still caused error, after doing bit more research on json parse, found solution, , writing following code gave me requested information.
console.log(json.parse(body)["result_info"]["total_pages"]);
the main thing property bracket notations needed outside json parse function.
thank provided here , helping me come solution.
Comments
Post a Comment