0

I get data in node js like this

var no_ktp = req.body.cust_noktp
request({
   uri : 'https://evodms-dev.client.com/evoDMM/api/api_customer_all.php?cust_noktp='+no_ktp,
   auth: {
      'bearer': '71D55F995678929'
   },
   rejectUnauthorized: false,//add when working with https sites
   requestCert: false,//add when working with https sites
   agent: false,//add when working with https sites

   }, function(error, rows) {
     if (error) {
       console.log(error);
     } else {
       console.log(rows);
     }

 });

and the log is like this

console.log(rows)

but i want to get the status : false, when i try console.log(rows.body.status) the log show undefined. how to write it so I get the status?

Thank you for any help...

4
  • can you tell me the output for this console.log(rows.constructor) Commented Sep 26, 2018 at 9:49
  • what response do you get for console.log(rows.body)? Commented Sep 26, 2018 at 9:50
  • try this.. var result = rows.toJSON() and after this console rows. if it doesn't work try var result = rows.body.toJSON() Commented Sep 26, 2018 at 9:52
  • Your callback function can take a third argument which is the body, see my answer below and the readme : npmjs.com/package/request Commented Sep 26, 2018 at 9:58

2 Answers 2

2

You could access the status using this way rows.body.statusif and only if your body is an object !

However, in the log, your object is actually a string, so, you need to first use JSON.parse(rows.body) in order to transform your object-string into an actual javascript object. Then you will be able to access any properties using the normal syntax

let body = JSON.parse(rows.body);
console.log(body.status) // 'False'

UPDATE

SO, I just read the readme for the request nodejs package https://www.npmjs.com/package/request And you can see that the request's callback take actually 3 arguments :

(error, response, body)

The response arguments si what you called 'rows', so you can, instead of using my method (JSON.parse) you can use instead the third argument provided to the callback function, no need to parse, you can use it directly :)

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like your body response is not in JSON format but string. You must convert it to JSON by

var no_ktp = req.body.cust_noktp
request({
   uri : 'https://evodms-dev.client.com/evoDMM/api/api_customer_all.php?cust_noktp='+no_ktp,
   auth: {
      'bearer': '71D55F995678929'
   },
   rejectUnauthorized: false,//add when working with https sites
   requestCert: false,//add when working with https sites
   agent: false,//add when working with https sites

   }, function(error, rows) {
     if (error) {
       console.log(error);
     } else {
       const body = JSON.parse(rows.body); // parse string to JSON
       console.log(body.status);
     }

 });

1 Comment

Well, JSON is always a string. Your code takes a JSON string and converts it to a javascript object. Converting a string to JSON does not make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.