【问题标题】:Error using Create-react-app with googleapis将 Create-react-app 与 googleapis 一起使用时出错
【发布时间】:2018-07-25 19:28:32
【问题描述】:

我是 React 的新手,我按照 Heroku 的指南使用 Create-react-app 创建了一个新的 React 应用程序。我想使用 Google APIs 通过 Google 的 OAuth2 对用户进行身份验证并获取刷新令牌,所以我按照 Google APIs Node Client 上的指南使用npm install googleapis --save 安装“googleapis”包,但是在我使用import {google} from 'googleapis' 导入 googleapis 之后,我当我点击npm start时出现以下错误:

Uncaught TypeError: Cannot convert undefined or null to object
at Function.getPrototypeOf (<anonymous>)
at ./node_modules/google-p12-pem/node_modules/pify/index.js.module.exports (index.js:75)
at Object../node_modules/google-p12-pem/build/src/index.js (index.js:6)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/gtoken/build/src/index.js (index.js:50)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/auth/jwtclient.js (jwtclient.js:63)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/auth/googleauth.js (googleauth.js:60)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/index.js (index.js:18)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/googleapis/build/src/lib/googleapis.js (googleapis.js:60)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/googleapis/build/src/index.js (index.js:18)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../src/App.js (App.css?9a66:26)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../src/index.js (index.css?f255:26)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object.0 (registerServiceWorker.js:117)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at ./node_modules/ansi-regex/index.js.module.exports (bootstrap 8c14a9046e11e185076c:724)
at bootstrap 8c14a9046e11e185076c:724

我错过了什么导致这个错误?

【问题讨论】:

标签: javascript node.js reactjs google-api create-react-app


【解决方案1】:

您可以使用 npm 模块“gapi-client”而不是“googleapis”。

做一个

npm install gapi-client

并在您的代码中使用类似,

import gapi from 'gapi-client';

它工作正常,您可以使用与谷歌示例中提到的相同的示例,例如https://developers.google.com/api-client-library/javascript/start/start-js

【讨论】:

    【解决方案2】:

    这里的错误是在客户端上渲染它,而不是在服务器端上。 googleapis 库并不是要在浏览器上运行,而是要在您的节点应用服务器中运行。

    Here is the full answer (github)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2017-10-26
      • 2021-01-30
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      相关资源
      最近更新 更多