Fulfill request.postAsync request using bluebird

I use the bluebird promises structure to execute a POST request and get a response to this 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 I get:

 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 need to do to resolve this error message?

Note: the version of bluebird that I am using now is 3.0.5

+5
source share
2 answers

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

Complete 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]) }); 
+9
source

Here is the answer that works: use the request-promise framework. I use request-promise@1.0.2 , which is based on bluebird@2.10.2. Obviously, something undocumented happened between bluebird@2.10.2 and bluebird@3.0.5

 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 }); 
+1
source

Source: https://habr.com/ru/post/1235358/


All Articles