I am using request-promise node module. I have been following the docs, and believe I should have set everything up correctly, however, I am getting the following error:
Unhandled rejection StatusCodeError: 400 - "{\n \"error\" : {\n
\"status\" : 400,\n \"message\" : \"invalid id\"\n }\n}" at new StatusCodeError (/Users/fitz035/Desktop/sony/travelLand/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:32:15)
My code looks like the following.
var request = require('request');
var rp = require('request-promise');
rp('apiOne' + mood)
.then(function(error, response, body) {
console.log(body)
}
}).then(function(error) {
for (var i = 0; i < array; i++) {
rp('apiTwo', function(error, response, body) {
console.log(body))
});
}
}).then(function(error, response, body) {
rp('apiThree' + songListUrl, function(error, response, body) {
console.log(body))
});
})
.catch(function(err) {
console.log(err)
});
EDIT
Is this the correct approach?
var options = {
uri: 'https://jsonplaceholder.typicode.com/posts/1',
json: true
}
rp(options).then(function(data){
console.log(data)
var optionsTwo = {
uri: 'http://www.google.com',
}
rp(optionsTwo).then(function(data){
console.log(data)
console.log(answer);
})
})
You're calling rp
as though it were request
in a couple of places, such as in your for
loop. Since you're not doing anything with the return value of those calls, you're not handling either resolution or rejection of those promises, thus getting the error warning you that there was a rejection you didn't handle.
The calls that you do use the return value from look incorrect as well. The callbacks you're passing in declare NodeJS-callback style argument lists, but that's not how promises work. Instead of a single callback that gets passed an error or null
as the first argument, you register separate callbacks for resolution (success) and rejection (error).
I suggest studying the examples on the request-promise
page in detail.
Re your follow-up:
Is this the correct approach?
var options = { uri: 'https://jsonplaceholder.typicode.com/posts/1', json: true } rp(options).then(function(data){ console.log(data) var optionsTwo = { uri: 'http://www.google.com', } rp(optionsTwo).then(function(data){ console.log(data) console.log(answer); }) })
No, that still doesn't handle rejections. If your goal is to do those two requests in series (not in parallel), then see comments:
var options = {
uri: 'https://jsonplaceholder.typicode.com/posts/1',
json: true
};
rp(options)
.then(function(data){
console.log(data)
var optionsTwo = {
uri: 'http://www.google.com'
};
return rp(optionsTwo).then(function(data){ // Note the return
console.log(data)
console.log(answer); // Don't know where `answer` is coming from...?
// Note that by not returning anything, you convert
// the resolution value to `undefined`. That's fine if
// you don't need it, but it's worth pointing out.
});
})
.catch(function(error) {
// One of the two operations above failed
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With