googleapis / nodejs-common

🚀🐢 A set of classes and utilities used in Google npm modules.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Service.request throws

kjin opened this issue · comments

Service.request throws an error:

TypeError: Cannot read property 'body' of undefined
    at (...)/Documents/cloud-trace-nodejs/node_modules/@google-cloud/common/src/service.ts:227(...)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

The line in question is:

https://github.com/googleapis/nodejs-common/blob/v0.18.4/src/service.ts#L227

My theory is that it's trying to read the body property from a field that was already a body property of another object. In other words, I believe this is a case where (err, body, resp) and (err, resp, body) has been mixed up. This is because tracing back to where the undefined object was born yields here:

https://github.com/googleapis/nodejs-common/blob/v0.18.4/src/util.ts#L323-L327

I'd love to help fix it but I'm not exactly sure what makeAuthenticatedRequest is doing. This section seems to pass a callback of type OnAuthenticatedCallback = (err: Error|null, reqOpts?: DecorateRequestOptions) => void when it really expects a type RequestCallback = (err: Error|null, body: ResponseBody, res: r.Response) => void, and the compiler seems to be OK with that (possible because type ResponseBody = any):

https://github.com/googleapis/nodejs-common/blob/v0.18.4/src/util.ts#L602-L603

I think ultimately that the compiler didn't catch this is the culprit, but I'm not sure what the correct thing should be.

Environment details

  • OS: macOS
  • Node.js version: 8
  • npm version: 5
  • @google-cloud/common version: 0.18.4

Steps to reproduce

  1. In the cloud-trace-nodejs repo: npm install @google-cloud/common@0.18.4 followed by npm run compile, then run $(npm bin)/mocha build/test/test-config-credentials.js