Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing request.postAsync promise with bluebird

I am using the bluebird promises framework to make a POST request and get the response to that POST request:

var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));

// Set the headers
    var headers = {
      'User-Agent':       'Super Agent/0.0.1',
      'Content-Type':     'application/x-www-form-urlencoded'
    }

 var options = [];
 var scores = [];

// Configure the request
options[0] = {
  url: 'https://api.havenondemand.com/1/api/sync/analyzesentiment/v1',
  method: 'POST',
  headers: headers,
  form: {'apikey': 'XXXXXXXXXXX', 'text': 'I love dogs'}
}

// Start the request
request.postAsync(options[0]).spread(function(response, body) {
  if (response.statusCode == 200) {
    var answer = JSON.parse(body);
    scores[0] = answer['aggregate']['score'];
  }
}).then(function() { console.log(scores[0]) });

This is the error message that I am getting:

Unhandled rejection TypeError: expecting an array or an iterable object but got [object Null]
    at apiRejection (/Users/vphuvan/demos/node_modules/bluebird/js/release/promise.js:10:27)
    etc.

What do I have to do to resolve this error message?

Note: the version of bluebird I am currently using is 3.0.5

like image 301
Vietnhi Phuvan Avatar asked Nov 06 '15 10:11

Vietnhi Phuvan


2 Answers

You need to set multiArgs: true in bluebird 3. This is one of the changes in the promisify API of bluebird 3.

Full solution below.

var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'), { multiArgs: true });

// Set the headers
    var headers = {
      'User-Agent':       'Super Agent/0.0.1',
      'Content-Type':     'application/x-www-form-urlencoded'
    }

 var options = [];
 var scores = [];

// Configure the request
options[0] = {
  url: 'https://api.havenondemand.com/1/api/sync/analyzesentiment/v1',
  method: 'POST',
  headers: headers,
  form: {'apikey': 'XXXXXXXXXXX', 'text': 'I love dogs'}
}

// Start the request
request.postAsync(options[0]).spread(function(response, body) {
  if (response.statusCode == 200) {
    var answer = JSON.parse(body);
    scores[0] = answer['aggregate']['score'];
  }
}).then(function() { console.log(scores[0]) });
like image 182
Mike Avatar answered Oct 30 '22 17:10

Mike


Here is an answer that works: use the 'request-promise' framework. I am using [email protected], which is based on [email protected]. Clearly, something undocumented happened between [email protected] and [email protected]

var rp = require('request-promise');

// Set the headers
var headers = {
  'User-Agent':       'Super Agent/0.0.1',
  'Content-Type':     'application/x-www-form-urlencoded'
}

var options = [];
var scores = [];
var text = 'I love dogs';

// Configure the request
options[0] = {
  url: 'https://api.havenondemand.com/1/api/sync/analyzesentiment/v1',
  method: 'POST',
  headers: headers,
  form: {'apikey': 'XXXXXXXXXX', 'text': text}
}

// Start the request

rp(options[0])
  .then(function (body) {
    // POST succeeded... 
    console.log(body);
    var answer = JSON.parse(body);
    scores[0] = answer['aggregate']['score'];
   })
  .then(function() { console.log(scores[0]); })
  .catch(function (err) {
      throw err
   });
like image 41
Vietnhi Phuvan Avatar answered Oct 30 '22 19:10

Vietnhi Phuvan