-1

i need to call external api with send body data and get reprocess.

i try:

var data = {};
data['data'] = {};
data['data']['access_token'] = '123456';
data['data']['secret_key'] = '456897';
data['data']['pincode'] = '457821';

var options = {
    "headers": { "content-type": "application/json" },
    "url": "http://example.com/api.php",
    "body": JSON.stringify(data)
};

const getChuckNorrisFact = util.promisify(Request);
getChuckNorrisFact(options).then(data => {
    let content = JSON.parse(data.body);
    console.log('Joke: ', data.body);   
}).catch(err => console.log('error: ', err))

console.log(getChuckNorrisFact);

i want to get response in getChuckNorrisFact variable.

  • @jonrsharpe it's not duplicate it's not about ajax or JavaScript it's about node.js in you not know Node.js then leave it. – mevi zewip Jul 06 '19 at 10:37
  • Node *is* JavaScript... Please **read** the proposed duplicate, it carefully explains this general class of problem and the various methods for dealing with it. – jonrsharpe Jul 06 '19 at 10:38
  • You cannot do what you're trying to do. Asynchronous promise results are ONLY available in the `.then()` callback or a function you call from there (it's an issue of timing). If you want, you can wrap this code in a function and use `await`, but that still won't let you assign the value to a higher scoped variable and use it the way you're trying to. You will need to learn how to proper asynchronous programming in Javascript (same techniques in browser or in node.js). Read the question yours is marked a dup of - all the techniques for getting the value out of the callback are described there. – jfriend00 Jul 06 '19 at 10:42
  • it's possible with another method ?? @jfriend00 – mevi zewip Jul 06 '19 at 10:51
  • No. Use the asynchronous value inside the callback to which it is delivered. That's how you program for asynchronous operations in Javascript. You can read about `async` and `await` which is a different looking syntax, but you will still have to learn about proper asynchronous programming. Just put the code that wants to use the `getChuckNorrisFact()` response INSIDE the `.then()` callback. That's how you do it. – jfriend00 Jul 06 '19 at 11:06
  • @jfriend00 i use `function doRequest(url) { return new Promise(function (resolve, reject) { request(url, function (error, res, body) { if (!error && res.statusCode == 200) { resolve(body); } else { reject(error); } }); }); } // Usage: async function main() { let res = await doRequest(url); console.log(res); } main();` but in that how to return from async function?? i not received value in `var res = main();` – mevi zewip Jul 06 '19 at 11:35
  • You can't directly return an asynchronous value from a function in Javascript. Cannot be done. You communicate back an asynchronous value via a promise or a callback or some other event notification scheme. Please read and study the question yours has been marked a duplicate of as all this is covered there. – jfriend00 Jul 06 '19 at 21:12

1 Answers1

-1

try with below code:

var data = {};
data['data'] = {};
data['data']['access_token'] = '123456';
data['data']['secret_key'] = '456897';
data['data']['pincode'] = '457821';

var options = {
    "headers": { "content-type": "application/json" },
    "url": "http://example.com/api.php",
    "body": JSON.stringify(data)
};

const request = util.promisify(Request);
request(options).then(data => {
    let getChuckNorrisFact = JSON.parse(data.body);
    console.log(getChuckNorrisFact);
}).catch(err => console.log('error: ', err))

or instead of converting request module to promise, you can directly use: https://www.npmjs.com/package/request-promise

ps: i haven't tried the code, so there may be need of some modifications. Hope this helps :)

Mohammed Amir Ansari
  • 2,311
  • 2
  • 12
  • 26