Search code examples
javascriptangularpromisees6-promise

Promise.then is not a function error only on server


I am running the below code on local which is working fine but on server I have this error:

Promise.then is not a function

let promise = null;
  if (this.fileUrl) {
    promise = this.pdfJS.getDocument(this.fileUrl);
  }
  else if (this.base64FileData) {
    const pdfData = atob(this.base64FileData);
    promise = this.pdfJS.getDocument({ data: pdfData });
  }
  console.log('pretty');
  console.log(promise);
  
  promise.then(function (docObj) {
    console.log('promistest');
    console.log(docObj);
    this._loadingEnd();
    this.renderPages(docObj);
  }, this._onDocumentLoadError.bind(this)
);

Solution

  • The object returned by .getDocument is not a promise, but it has a property called promise

    therefore

    let promise;
    if (this.fileUrl) {
        promise = this.pdfJS.getDocument(this.fileUrl).promise;
    } else if (this.base64FileData) {
        const pdfData = atob(this.base64FileData);
        promise = this.pdfJS.getDocument({ data: pdfData }).promise;
    } else {
        promise = Promise.reject('invalid state');
        // this will be handled by `this._onDocumentLoadError.bind(this)`
    }
    console.log('pretty');
    console.log(promise);
    promise.then(function (docObj) {
        console.log('promistest');
        console.log(docObj);
        this._loadingEnd();
        this.renderPages(docObj);
    }, this._onDocumentLoadError.bind(this));