title: (node:2684) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'then' of undefined
body: <p>I am using .then for first time, instead of .then I use callback function.</p>
  
  <p>Below is my code snippet:</p>
  
  <pre><code>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);
                  });
              });
          });
      });
  });
  </code></pre>
  
  <p>The above function is calling a function which is as below, this is in another file and called properly filename is waybill.js:</p>
  
  <pre><code>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);
              }
          });
      }
  };
  </code></pre>
  
  <p>Previously I was using this pattern to make callbacks and it was working fine:</p>
  
  <pre><code>waybill_url.insertWaybillUrl('Lpsimer', waybillUrl, function(err, success) {
     if (err) {
  
     } else {
  
     }
  )}
  </code></pre>
  
  <p>Now I have to use .then due to usage of phantom code to write PDF and it has made the job cumbersome. </p>
  
  <p>Need suggestion on how I can make callbacks within callbacks.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>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);
  });
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p>Cannot read property 'then' of undefined</p>
  </blockquote>
  
  <p>I am getting this error where I have added   console.log("error_render");
  that is where I am calling page.render function of phantom.</p>
  
  <p>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, <em>function name: waybill_url.insertWaybillUrl</em>. But due to callbacks and asynchronous behaviour this simple calling of two functions is getting cumbersome. </p>