Weird behaviour of request-json with bluebird promise - node.js

I'm trying to wrap my head around promises, but so far I can't seem to get simple example working. Here it a code to request JSON from the server:
module.exports = function (app, options) {
var promise = require('bluebird');
var request = require('request-json');
var module = {
url: options.url,
httpClient: promise.promisifyAll(request.createClient(options.url))
};
module.getSample = function() {
return this.httpClient.getAsync('sample/')
.then(function(error, response, body) {
console.log(body);
})
.catch(function(e) {
console.log('error');
console.log(e);
});
};
return module;
};
but when I call it like this:
var backendClient = require('./utils/backendClient.js')(app, {
url: 'http://localhost:8080/'
});
backendClient.getSample()
at runtime I get an error saying '[SyntaxError: Unexpected token o]'. Version without promises works fine. What did I miss?

module.getSample = function() {
return this.httpClient.getAsync('sample/')
.then(function(error, response, body) {
// not sure what Promise library you are using, but in the Promise/A+ spec, the function in then only receives a single argument, the resolved value of the Promise
console.log(body);
// this returns equivalent to Promise.resolve(undefined);
// you really want to return something meaningful here
})
.catch(function(e) {
console.log('error');
console.log(e);
// this also returns equivalent to Promise.resolve(undefined);
// to propagate the "error" condition, you want to either throw e, or return Promise.reject(something here);
});
};
This will always return a fullfilled promise with undefined as the value, never a rejected one. Other errors commented above

Related

(node:2684) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'then' of undefined

I am using .then for first time, instead of .then I use callback function.
Below is my code snippet:
phantom.create().then(function (ph) {
ph.createPage().then(function (page) {
page.open("http://codebeautify.org/xmlvalidator").then(function (status) {
page.render(base_pdf_path + lpnumber + '.pdf').then(function () {
console.log('PDF generated for waybill');
//Insert waybill url in db.
return waybill_url.insertWaybillUrl('Lpsimer', waybillUrl).then(function (waybill_inserted_resp) {
callback(null, true);
}).catch(function (error) {
callback(err_waybill_inserted);
});
});
});
});
});
The above function is calling a function which is as below, this is in another file and called properly filename is waybill.js:
var mongoose = require('mongoose');
var q = require('promised-io/promise');
var WaybillUrlSchema = new mongoose.Schema({
lpnumber: String,
url: String,
waybilltime: Date
});
module.exports = {
insertWaybillUrl: function (lpnumber, url) {
var defer = q.defer();
var waybill_insert = new waybill_url({
lpnumber: lpnumber,
url: url,
waybilltime: new Date()
});
//Save model to MongoDB
waybill_insert.save(function (err, inserted_waybill) {
if (err) {
return defer.reject(err);
}
else {
return defer.resolve(inserted_waybill);
}
});
}
};
Previously I was using this pattern to make callbacks and it was working fine:
waybill_url.insertWaybillUrl('Lpsimer', waybillUrl, function(err, success) {
if (err) {
} else {
}
)}
Now I have to use .then due to usage of phantom code to write PDF and it has made the job cumbersome.
Need suggestion on how I can make callbacks within callbacks.
UPDATE
phantom.create().then(function (ph) {
ph.createPage().then(function (page) {
page.open("http://codebeautify.org/xmlvalidator").then(function (status) {
page.render(base_pdf_path + lpnumber + '.pdf').then(function () {
//Insert waybill url in db.
waybill_url.insertWaybillUrl('Lpsimer', waybillUrl).then(function (waybill_inserted_resp) {
if (waybill_inserted_resp) {
callback(null, true);
}
}).catch(function (error_waybill_url_insert) {
console.log("Error in inserting waybill:" + err_waybill_inserted);
callback(error_waybill_url_insert);
});
}).catch(function (error_render) {
console.log("error_render");
callback(error_render);
});
}).catch(function (error_open) {
callback(error_open);
});
}).catch(function (error_create_page) {
callback(error_create_page);
});
}).catch(function (error_phantom_create) {
callback(error_phantom_create);
});
Now I have added catch for every then as suggested by rsp in his answer, but now I am getting error which I have catched and send to another callback:
Cannot read property 'then' of undefined
I am getting this error where I have added console.log("error_render");
that is where I am calling page.render function of phantom.
My requirement is simple. I want to generate a PDF file using phantom and then I just want to call another function which is in another file waybill_url, function name: waybill_url.insertWaybillUrl. But due to callbacks and asynchronous behaviour this simple calling of two functions is getting cumbersome.
Make sure that you use catch() and not only then() - or two arguments to then(). Otherwise you will not handle errors and you will get that warning - which will be an error, not a warning, in next versions of Node.
See this answer for more info about it:
Should I refrain from handling Promise rejection asynchronously?

Promise is undefined in procedural code in nodejs

I'm very new to async languages like nodejs, I'm trying to write a web scraper that will visit a link, download a code, extract with regex, THEN visit another link using that code. I'm aware I could use callbacks, but I expect to have to go 8-9 levels deep, I think promises is the way to go (is there a better way?)
var promise = require("promise");
var request = require("request");
login();
function get_login_code()
{
request.get("someurl.com", function (error, response, body)
{
// just for example
body = 'TOKEN" value="hello world"';
var login_code = body.match(/.TOKEN" value="([^"]+)../i);
return login_code
});
}
function login()
{
var login_promise = promise.resolve(get_login_code());
console.log(login_promise);
}
I've tried a bunch of combinations of messing around with promises, but I either always get undefined or a promise which doesn't have a value. I don't want to nest promise functions inside promises because that's exactly the same thing as callback hell. Can someone tell me what I'm doing wrong, I really want this code to be procedural and not 8 callbacks. In the ideal world promise.resolve just waits until get_login_code() returns the actual code, not undefined.
Output:
Promise { _45: 0, _81: 1, _65: undefined, _54: null }
Desired Output:
hello world
What your code do:
calls get_login_code that returns nothing (i.e. undefined)
inside of login function you create a new promise that is immediately resolved to the result of get_login_code, i.e. undefined.
Thus, you do not use login_code at all.
To make it work, you should make get_login_code to return a promise that will be resolved to login_code. Consider you use promise npm module, the code may look like:
// uppercased, it's a constructor
var Promise = require("promise");
var request = require("request");
login();
function get_login_code()
{
return new Promise(function (resolve, reject) {
request.get("someurl.com", function (error, response, body) {
if (err) {
reject(err);
return;
}
// just for example
body = 'TOKEN" value="hello world"';
var login_code = body.match(/.TOKEN" value="([^"]+)../i);
resolve(login_code);
});
});
}
function login()
{
// return a new promise to use in subsequent operations
return get_login_code()
.then(function(login_code) {
console.log(login_code);
});
}
You should create new promise in the function to handle reject and resolve not by handling resolve to the function itself. Use then to get the response value from promise. I guess this should work.
var promise = require("promise");
var request = require("request");
function get_login_code()
{
var promise = new Promise(function(resolve, reject) {
request.get("someurl.com", function (error, response, body)
{
if (error) {
reject(error);
} else {
// just for example
body = 'TOKEN" value="hello world"';
var login_code = body.match(/.TOKEN" value="([^"]+)../i);
resolve(login_code);
}
});
});
}
get_login_code()
.then(function (code) {
console.log(code);
});

Creating functions to chain promises correctly

I am trying to get Promise chaining working for me correctly.
I believe the problem boils down to understanding the difference between:
promise.then(foo).then(bar);
and:
promise.then(foo.then(bar));
In this situation I am writing both foo and bar and am trying to get the signatures right. bar does take a return value that is produced by foo.
I have the latter working, but my question is what do I need to do to get the former working?
Related to the above is the full code (below). I don't have the different logs printed in the order I am expecting (expecting log1, log2, log3, log4, log5, but getting log3, log4, log5, log1, log2). I am hoping as I figure the above I will get this working right as well.
var Promise = require('bluebird');
function listPages(queryUrl) {
var promise = Promise.resolve();
promise = promise
.then(parseFeed(queryUrl)
.then(function (items) {
items.forEach(function (item) {
promise = promise.then(processItem(transform(item)))
.then(function() { console.log('log1');})
.then(function() { console.log('log2');});
});
}).then(function() {console.log('log3')})
).then(function() {console.log('log4')})
.catch(function (error) {
console.log('error: ', error, error.stack);
});
return promise.then(function() {console.log('log5');});
};
What is the difference between promise.then(foo).then(bar); and promise.then(foo.then(bar));?
The second one is simply wrong. The then method takes a callback as its argument, not a promise. That callback might return a promise, so the first one is equivalent to
promise.then(function(x) { return foo(x).then(bar) })
(assuming that foo returns a promise as well).
Your whole code appears to be messed up a bit. It should probably read
function listPages(queryUrl) {
return parseFeed(queryUrl)
.then(function (items) {
var promise = Promise.resolve();
items.forEach(function (item) {
promise = promise.then(function() {
console.log('log1');
return processItem(transform(item));
}).then(function() {
console.log('log2');
});
});
return promise;
}).then(function() {
console.log('log3')
}, function (error) {
console.log('error: ', error, error.stack);
});
}

Use promises for multiple node requests

With the request library, is there a way to use promises to simplify this callback?
var context = {};
request.get({
url: someURL,
}, function(err, response, body) {
context.one = JSON.parse(body);
request.get({
url: anotherURL,
}, function(err, response, body) {
context.two = JSON.parse(body);
// render page
res.render('pages/myPage');
});
});
Here's a solution using the Bluebird promises library. This serializes the two requests and accumulates the results in the context object and rolls up error handling all to one place:
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"), {multiArgs: true});
var context = {};
request.getAsync(someURL).spread(function(response, body) {
context.one = JSON.parse(body);
return request.getAsync(anotherURL);
}).spread(response, body)
context.two = JSON.parse(body);
// render page
res.render('pages/myPage');
}).catch(function(err) {
// error here
});
And, if you have multiple URLs, you can use some of Bluebirds other features like Promise.map() to iterate an array of URLs:
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"), {multiArgs: true});
var urlList = ["url1", "url2", "url3"];
Promise.map(urlList, function(url) {
return request.getAsync(url).spread(function(response,body) {
return [JSON.parse(body),url];
});
}).then(function(results) {
// results is an array of all the parsed bodies in order
}).catch(function(err) {
// handle error here
});
Or, you could create a helper function to do this for you:
// pass an array of URLs
function getBodies(array) {
return Promise.map(urlList, function(url) {
return request.getAsync(url).spread(function(response.body) {
return JSON.parse(body);
});
});
});
// sample usage of helper function
getBodies(["url1", "url2", "url3"]).then(function(results) {
// process results array here
}).catch(function(err) {
// process error here
});
Here is how I would implement chained Promises.
var request = require("request");
var someURL = 'http://ip.jsontest.com/';
var anotherURL = 'http://ip.jsontest.com/';
function combinePromises(context){
return Promise.all(
[someURL, anotherURL].map((url, i)=> {
return new Promise(function(resolve, reject){
try{
request.get({
url: url,
}, function(err, response, body) {
if(err){
reject(err);
}else{
context[i+1] = JSON.parse(body);
resolve(1); //you can send back anything you want here
}
});
}catch(error){
reject(error);
}
});
})
);
}
var context = {"1": "", "2": ""};
combinePromises(context)
.then(function(response){
console.log(context);
//render page
res.render('pages/myPage');
}, function(error){
//do something with error here
});
Doing this with native Promises. It's good to understand the guts.
This here is known as the "Promise Constructor Antipattern" as pointed out by #Bergi in the comments. Don't do this. Check out the better method below.
var contextA = new Promise(function(resolve, reject) {
request('http://someurl.com', function(err, response, body) {
if(err) reject(err);
else {
resolve(body.toJSON());
}
});
});
var contextB = new Promise(function(resolve, reject) {
request('http://contextB.com', function(err, response, contextB) {
if(err) reject(err);
else {
contextA.then(function(contextA) {
res.render('page', contextA, contextB);
});
}
});
});
The nifty trick here, and I think by using raw promises you come to appreciate this, is that contextA resolves once and then we have access to it's resolved result. This is, we never make the above request to someurl.com, but still have access to contextA's JSON.
So I can conceivable create a contextC and reuse the JSON without having to make another request. Promises always only resolve once. You would have to take that anonymous executor function out and put it in a new Promise to refresh that data.
Bonus note:
This executes contextA and contextB in parallel, but will do the final computation that needs both contexts when both A & B are resolved.
Here's my new stab at this.
The main problem with the above solution is none of the promises are reusable and they are not chained which is a key feature of Promises.
However, I still recommend promisifying your request library yourself and abstaining from adding another dependency to your project. Another benefit of promisifying yourself is you can write your own rejection logic. This is important if you're working with a particular API that sends error messages in the body. Let's take a look:
//Function that returns a new Promise. Beats out constructor anti-pattern.
const asyncReq = function(options) {
return new Promise(function (resolve, reject) {
request(options, function(err, response, body) {
//Rejected promises can be dealt with in a `catch` block.
if(err) {
return reject(err);
}
//custom error handling logic for your application.
else if (hasError(body)) {
return reject(toError(body));
}
// typically I just `resolve` `res` since it contains `body`.
return resolve(res);
}
});
};
asyncReq(urlA)
.then(function(resA) {
//Promise.all is the preferred method for managing nested context.
return Promise.all([resA, asyncReq(urlB)]);
})
.then(function(resAB) {
return render('page', resAB[0], resAB[1]);
})
.catch(function(e) {
console.err(e);
});
You can use the request-promise library to do this. In your case, you could have something like this, where you chain your requests.
request
.get({ url: someURL })
.then(body => {
context.one = JSON.parse(body);
// Resolves the promise
return request.get({ url: anotherURL });
})
.then(body => {
context.two = JSON.parse(body);
res.render('pages/myPage');
})
.catch(e => {
//Catch errors
console.log('Error:', e);
});
By far the easiest is to use request-promise library. You can also use use a promise library like bluebird and use its promisify functions to convert the request callback API to a promise API, though you may need to write your own promisify function as request does not use the standard callback semantics. Lastly, you can just make your own promise wrapper, using either native promises or bluebird.
If you're starting fresh, just use request-promise. If you're refactoring existing code, I would just write a simple wrapper for request using bluebird's spread function.

Clean down the index before each text (Mocha, Elastic)

How do we clear down the index before each test? (at the moment it fails my test, with a timeout)
I have tried
DeleteBy (I have tried the term and q)
http delete
delete the index
I have the following code:
var assert = require("assert"),
elasticsearch = require('elasticsearch'),
request = require('request'),
q = require('q'),
client;
client = new elasticsearch.Client();
describe('people', function(){
beforeEach(function(done){
//is this correct?
//looks like poeple get deleted.
client.deleteByQuery({
index: 'people',
q: '*'
}, function (error, response) {
done();
});
//I have also tried the following:
//this way returns an accept code.
// var deleteOptions = {
// method: 'DELETE',
// uri: 'http://localhost:9200/people/person'
// };
//
// webApi(deleteOptions).then(function(data){
// //{"acknowledged":true}
// console.log(data.body);
// done();
// });
//the following throws an exception
//delete index
// client.indices.delete('people').then(function(del){
// console.log(del);
// client.indices.create('people').then(function(ct){
// console.log(ct);
// done();
// });
// });
});
describe('add a entry into the index', function(){
it('should add Dave as a person to the database', function(done){
//THIS TEST WILL FAIL
//Error: timeout of 2000ms exceeded
//I have tried adding a timeout.
assert.equal(1,1);
});
});
});
var webApi = function(options){
var deferred = q.defer();
var handle = function (error, response, body) {
//console.log(body);
if(error) {
deferred.reject(error);
}
else {
deferred.resolve({response:response, body:body});
}
};
request(options, handle);
return deferred.promise; //NOTE: we now return a promise
};
The test that you marked with THIS TEST WILL FAIL fails with a timeout because you never call done() in it. You've declared the anonymous function you pass to it with a parameter, which tells Mocha that your test is asynchronous (even if the code in it is not asynchronous right now) and thus Mocha waits for done() to be called. You can either call it, or remove the parameter from your function.

Resources