hisco / http2-client

Transparently make http request to both http1 / http2 server.
https://www.npmjs.com/package/http2-client
MIT License
33 stars 14 forks source link

Uncaught TypeError: Cannot read property 'prototype' of undefined #25

Closed snowdream closed 4 years ago

snowdream commented 4 years ago

When i use http2-client and got in the vue project. i get an error as follows:

request.js:11 Uncaught TypeError: Cannot read property 'prototype' of undefined
    at addFunctions (request.js:11)
    at Object.<anonymous> (request.js:24)
    at Object../node_modules/http2-client/lib/request.js (request.js:568)
    at __webpack_require__ (bootstrap:723)
    at fn (bootstrap:100)
    at Object../node_modules/http2-client/lib/index.js (index.js:3)
    at __webpack_require__ (bootstrap:723)
    at fn (bootstrap:100)
    at Object../src/utils/request2.js (request2.js:8)
    at __webpack_require__ (bootstrap:723)

in the code :(request.js)

function addFunctions(container , obj){
  const proto = obj.prototype;   //error 
  Object.keys(proto).forEach((name)=>{
    if (container.indexOf(name)!=-1)
    return;
    if (name.indexOf('_')!=0 && typeof proto[name] == 'function'){
      container.push(name);
    }
  })
}
DC44D394-1589-4D43-A5C6-9093BCC8FC46
hisco commented 4 years ago

It appears you running this lib has is in the browser. Notice that Node.JS built in modules cannot run in browser, this reason you are getting the error is because in the following line:

addFunctions(STUBBED_METHODS_NAME, http.ClientRequest);

http module doesn't have ClientRequest which will never happen in Node.JS env.