0

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.

Jehanzeb.Malik
  • 3,332
  • 4
  • 25
  • 41
Always_a_learner
  • 4,585
  • 13
  • 63
  • 112

1 Answers1

3

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:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • I have made an update. Please help me to move further as now I am getting an error in catch that cannot call then on undefined. I know this error is common and have many solutions on internet but I am unable to resolve. Any help would appreciated. Thanks. – Always_a_learner Feb 23 '17 at 06:08
  • @Simer were you able to solve this issue? I'm curious on how you fixed it. Thanks – Oliver Jun 29 '17 at 16:45
  • @oliver I just shifted return waybill_url.insertWaybillUrl to another function so that I don't have to call any other function after .then. This solved my problem for time being. Therefore after "console.log('PDF generated for waybill');" I wrote only callback(null, true). Sorry but did not find any definite solution for this error and neither I found out the reason for it. – Always_a_learner Jul 03 '17 at 11:13