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.