简体   繁体   English

使用aurelia-fetch-client时发生TypeError

[英]TypeError when using aurelia-fetch-client

I am using the aurelia-fetch-client version 1.0.1. 我正在使用aurelia-fetch-client版本1.0.1。

I execute the fetch within a service class that I have created, and I get an error stating: 我在创建的服务类中执行提取,并且出现错误提示:

TypeError: Cannot read property 'isProtoTypeOf' of undefined at eval [filepath]aurelia-fetch-client. TypeError:无法在eval [filepath] aurelia-fetch-client读取未定义的属性'isProtoTypeOf'。

The actual error is being thrown in the file aurelia-fetch-client on line 134. the code there is: 在第134行的aurelia-fetch-client文件中引发了实际错误。代码如下:

This code is from 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);
    });
  });

See above. 往上看。 I've annotated the line of code throwing the error. 我已经注释了引发错误的代码行。 The "prototype" is null, so it is returning the given error message. “原型”为null,因此它正在返回给定的错误消息。

I had all of this working in a previous project, but using the beta version of the aurelia-fetch-client. 我在上一个项目中完成了所有这些工作,但是使用的是beta版本的aurelia-fetch-client。 The code below is very similar to the code calling the beta version of the aurelia-fetch-client. 下面的代码与调用aurelia-fetch-client beta版本的代码非常相似。

The documentation states that I need to load a polyfill for this, so I load the "fetch" polyfill as specified (I'm doing this in main.js), but still keep getting this error. 文档指出,我需要为此加载一个polyfill,因此我按指定的方式加载了“ fetch” polyfill(我正在main.js中这样做),但是仍然不断收到此错误。 The same thing happens if I load it here in the service. 如果我将其加载到服务中,也会发生同样的事情。

Has anyone every experienced this, and how have you solved it? 每个人都有经历过,您如何解决?

Here is some code of the service call and the base service: 这是服务调用和基本服务的一些代码:

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

    }

}

and the service that extends this base class: 以及扩展该基类的服务:

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

    }

}

A little bit more information regarding the overall project is that this is being built inside of an existing ASP.NET MVC project. 有关整个项目的更多信息是,这是在现有ASP.NET MVC项目内部构建的。 A new area is being created where the new set of screens will live which can also allow nice encapsulation of the files and dependencies, but utilize the existing service layer that provides the data interfaces. 正在创建一个新区域,将在其中放置新的屏幕集,该区域还可以很好地封装文件和相关性,但是可以利用提供数据接口的现有服务层。

For those of you who may come across this (or something similar), I've found the source of the issue. 对于可能遇到此问题(或类似问题)的人们,我已经找到了问题的根源。

This did not have anything to do with the polyfill. 这与polyfill没有任何关系。 The JS code worked in the latest version of Chrome (v 54.xxx). JS代码可在最新版本的Chrome(v 54.xxx)中使用。 And because it was not an older browser, the polyfill was not at fault. 而且由于它不是较旧的浏览器,因此polyfill不会出错。 What WAS the problem was a javascript file clash. 问题出在javascript文件冲突。

The "Response" object was being overriddenn somehow and I had to determine where. “响应”对象以某种方式被覆盖,我必须确定在哪里。 Since this is living inside of a larger ASP.NET MVC application I looked up application tree to the main application's script files and found that there was, indeed, a response.js file being used. 由于它位于较大的ASP.NET MVC应用程序内部,因此我在主应用程序的脚本文件中查找了应用程序树,并发现确实使用了response.js文件。 It was implemented long ago to help in accessing the service layer of our application suite. 它是在很早以前实施的,以帮助访问我们应用程序套件的服务层。

When this was removed, the aurelia-fetch-client worked as advertised. 删除此内容后,aurelia-fetch-client按照广告中的说明工作。 The problem now is that some of the existing code accessing the services does not work properly. 现在的问题是,访问服务的某些现有代码无法正常工作。 The solution for that is beyond the scope of this write up. 解决方案超出了本文的范围。 Bottom line, it was JS clashing causing the problems. 最重要的是,这是JS冲突引起的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM