Не удается прочитать свойство https неопределенной ошибки в функциях Firebase - PullRequest
0 голосов
/ 01 июля 2018

Я следую этому официальному учебнику firebase на YouTube по облачным функциям для рендеринга реакции на стороне сервера.

(Кстати, я следовал точной структуре каталогов как видео.)

Но я получаю сообщение об ошибке всякий раз, когда пытаюсь его обслужить:

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"
  }
}

Глядя на ошибку, она указывает на строку functions.https.onRequest(app); в файле index.js .

Я также проверил в руководстве по началу работы с функциями , и он имеет functions.https.onRequest(...).

Итак, где проблема и как я могу ее решить?

Сведения о системе :

macOS: 10.13.4

нвм: 0,33,11

узел: 6.11.5

нпм: 3.10.10

1 Ответ

0 голосов
/ 01 июля 2018

Вы должны сделать:

import * as functions from 'firebase-functions'

потому что 'firebase-functions' явно не имеет экспорта по умолчанию

или вы можете сделать:

import { https } from 'firebase-functions'

но не забудьте добавить к этому импоту любые другие функции, которые вы можете использовать в своем коде.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...