【问题标题】:Cannot read property https of undefined error in firebase functions无法读取 Firebase 函数中未定义错误的属性 https
【发布时间】:2018-12-09 15:56:57
【问题描述】:

我正在关注这个youtube firebase official tutorial on cloud functions 进行服务器端反应渲染。

(顺便说一下,我按照视频的确切目录结构。)

但每当我尝试提供它时都会出错:

i  functions: Preparing to emulate functions.
⚠  hosting: Port 5000 is not available. Trying another port...
i  hosting: Serving hosting files from: dev
✔  hosting: Local server: http://localhost:5004

⚠  functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
⚠  functions: Error from emulator. Error occurred while parsing your function triggers.

TypeError: Cannot read property 'https' of undefined
    at Object.<anonymous> (/Users/mac/Documents/React-Projects/React-js/Cards/functions/index.js:35:54)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /Users/mac/.nvm/versions/node/v6.11.5/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11
    at Object.<anonymous> (/Users/mac/.nvm/versions/node/v6.11.5/lib/node_modules/firebase-tools/lib/triggerParser.js:75:3)

我已经尝试npm i --save firebase-functions,但仍然出现错误。

firebase.json:

{
  "hosting": {
    "public": "dev",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites":[{
      "source": "**",
      "function": "main"
    }]
  }
}

index.js 在项目文件夹的根目录中:

import React from 'react';
import { renderToString } from 'react-dom';
import express from 'express';
import Test from './src/components/Test/Test';
import functions from 'firebase-functions';

const app = express();

app.get('**', (req, res) => {
    const html = renderToString(<Test />);
    res.send(html);
});

export let main = functions.https.onRequest(app);

package.json:

{
  "name": "Cards",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack --mode development",
    "start": "webpack-dev-server --open --hot --mode development",
    "build": "webpack -p --config webpack.prod.config.js",
    "babel": "babel src -d functions/src && babel index.js -d functions"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@material-ui/core": "^1.3.0",
    "@material-ui/icons": "^1.1.0",
    "firebase": "^5.2.0",
    "react": "^16.4.1",
    "react-dom": "^16.4.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "firebase-admin": "^5.12.1",
    "firebase-functions": "^1.1.0",
    "firebase-tools": "^3.19.1",
    "webpack": "^4.14.0",
    "webpack-dev-server": "^3.1.4"
  }
}

通过查看错误,它指向 index.js 文件中的functions.https.onRequest(app); 行。

我还签入了functions get started guide,它有functions.https.onRequest(...)

那么,问题出在哪里,我该如何解决呢?

系统详情

macOS:10.13.4

非虚拟机:0.33.11

节点:6.11.5

npm: 3.10.10

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-functions


    【解决方案1】:

    你应该这样做:

    import * as functions from 'firebase-functions'
    

    因为 'firebase-functions' 显然似乎没有默认导出

    或者你可以这样做:

    import { https } from 'firebase-functions'
    

    但请记住添加您可能在代码中使用的任何其他函数。

    【讨论】:

    • 有效!但我还有一个疑问。当我访问localhost:5000 时,由于我在firebase.json? 中编写的重写,它应该正确运行我的函数?但它没有发生。
    • 您应该为此提出另一个问题,并提供更多详细信息,因为它超出了此问题的范围;)
    • 我明白了。在提出另一个问题之前,我会对此进行更多研究。
    • 我很乐意帮助你,但我对 firebase 了解不多,而且你缺乏细节,所以我相信其他人可以回答它:) 祝你好运
    猜你喜欢
    • 2020-03-26
    • 2020-08-01
    • 2019-04-05
    • 2018-10-30
    • 2018-04-01
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多