【发布时间】:2016-10-19 18:49:07
【问题描述】:
我正在使用 aurelia-fetch-client 版本 1.0.1。
我在我创建的服务类中执行提取,并收到一条错误消息:
TypeError:无法在评估时读取未定义的属性“isProtoTypeOf” [文件路径]aurelia-fetch-client.
实际的错误是在第 134 行的 aurelia-fetch-client 文件中抛出的。代码如下:
此代码来自 aurelia-fetch-client:
var promise = processRequest(request, this.interceptors).then(function (result) {
var response = null;
if (Response.prototype.isPrototypeOf(result)) { <<--PROBLEM IS HERE!!!! LINE 134
response = result;
} else if (Request.prototype.isPrototypeOf(result)) {
request = Promise.resolve(result);
response = fetch(result);
} else {
throw new Error('An invalid result was returned by the interceptor chain. Expected a Request or Response instance, but got [' + result + ']');
}
return request.then(function (_request) {
return processResponse(response, _this.interceptors, _request);
});
});
见上文。我已经注释了抛出错误的代码行。 “原型”为空,因此它返回给定的错误消息。
我在之前的项目中完成了所有这些工作,但使用的是 aurelia-fetch-client 的 beta 版本。下面的代码与调用 beta 版 aurelia-fetch-client 的代码非常相似。
documentation 声明我需要为此加载一个 polyfill,因此我按指定加载了“fetch” polyfill(我在 main.js 中执行此操作),但仍然出现此错误。如果我将它加载到服务中,也会发生同样的事情。
有没有人遇到过这种情况,你是怎么解决的?
这是服务调用和基础服务的一些代码:
import {inject} from 'aurelia-framework';
import {Configure} from 'aurelia-configuration';
import {HttpClient} from 'aurelia-fetch-client';
export class ServiceBase {
// *************************************************************
// Services Constructor
// Sets up the base http Client configuration
// *************************************************************
constructor(configure, httpClient) {
// Set up configuration and initial user token
this.userToken = 'tokenvalue'; //TODO: put real one in later, not used on this call
this.configure = configure;
this.httpClient = httpClient;
this.httpClient.configure(config => {
config
.withBaseUrl(this.configure.get('servicesBaseUrl'))
.withDefaults({
headers: {
'content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/x-www-form-urlencoded',
'X-Requested-With': 'Fetch'
},
mode: 'cors'
})
.withInterceptor({
request(request) {
console.log('KCU Class Requesting: ' + request.method + '; ' + request.url);
return request;
},
response(response) {
console.log('KCU Class Received: ' + response.status + '; ' + response.url);
if (response.status !== 200) {
throw response;
} else {
return response;
}
},
requestError(err) {
console.log('Request Error Receieved: ' + err.toString());
}
});
});
}
}
以及扩展此基类的服务:
import {inject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';
import {Configure} from 'aurelia-configuration';
import {ServiceBase} from './serviceBase'
@inject(Configure, HttpClient)
export class HospitalService extends ServiceBase {
constructor(configure, httpClient) {
super(configure, httpClient);
this.configure = configure;
}
// ****************************************
// Extracts the list of hospitals for the initial search grid
// ****************************************
getHospitalListSearchGridData() {
let urlCompletion = '';
return this.httpClient.fetch(
this.configure.get('HospitalSearch') + urlCompletion, // This is the method name on the service to call
{
method: 'get',
headers: {
'Authorization': 'Bearer ' + this.userToken.toString()
}
}
)
.then(response => response.json())
.then(responseInfo => {
return responseInfo;
})
.catch(error => {
return false;
});
}
}
关于整个项目的更多信息是,这是在现有的 ASP.NET MVC 项目中构建的。正在创建一个新区域,新的屏幕集将在其中存在,它还可以很好地封装文件和依赖项,但利用提供数据接口的现有服务层。
【问题讨论】:
-
所以您遇到的问题是
Request类未定义。就像你说的,解决这个问题的方法是通过 polyfill。很难说还有什么修复可能来自那里。我会验证 polyfill 正在加载,验证Request是否在全局命名空间中定义(通过断点),并在加载 polyfill 之前仔细检查上述代码是否被意外调用。 -
@matthewjames 实际上它是 Aurelia-Fetch-Client 代码中的 Response 对象。我检查了是否加载了 fetch.js。我实际上将它导入到 main.js 文件中。我尝试将它加载到具有实际 http 调用的类中,但遇到了同样的问题。我需要做一个额外的方法调用来加载它吗?
标签: javascript aurelia aurelia-fetch-client