Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Promise.then不仅仅是服务器上的函数错误_Javascript_Angular_Promise_Es6 Promise - Fatal编程技术网

Javascript Promise.then不仅仅是服务器上的函数错误

Javascript Promise.then不仅仅是服务器上的函数错误,javascript,angular,promise,es6-promise,Javascript,Angular,Promise,Es6 Promise,我正在本地运行以下代码,该代码运行正常,但在服务器上出现此错误: 那不是一个函数 .getDocument返回的对象不是承诺,但它有一个名为promise 所以 let promise; if (this.fileUrl) { promise = this.pdfJS.getDocument(this.fileUrl).promise; } else if (this.base64FileData) { const pdfData = atob(this.base64FileDa

我正在本地运行以下代码,该代码运行正常,但在服务器上出现此错误:

那不是一个函数


.getDocument
返回的对象不是承诺,但它有一个名为
promise

所以

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));

最简单的调试步骤是查看
console.log(promise)是什么输出您可以添加
else promise=promise.reject(“无效状态”)
然后让
处理此问题。\u onDocumentLoadError
处理错误,这意味着它不是承诺,因此没有
.then
方法-尝试
promise.promise.then
,因为
pdfJS.getDocument
返回一个具有
promise
属性的对象,如所示(不过,我刚刚意识到,无论您使用什么,这可能都不是正确的文档)-什么是
this.pdfJS
以及
this.pdfJS.getDocument
返回什么?好吧,很酷,那么现在问题完全无关了?我建议你试着看看有什么问题,然后问一个新问题,因为这个问题现在已经解决了。我已经添加了一个答案,因为这可能会让其他人在未来感到困惑
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));