如何使用ES6语法导入firebase

如何使用ES6语法导入firebase

本文介绍了如何使用ES6语法导入firebase-functions和firebase-admin以使用Babel进行Node 10的编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我目前正在用ES6编写我的云函数,并与Babel进行转译以针对Node v10环境.而且我注意到了一些奇怪的东西.

I'm currently writing my cloud functions in ES6 and transpiling with Babel to target the Node v10 environment. And I've noticed something weird.

为什么当我这样输入 firebase-functions 时是这样的:

Why is that when I import firebase-functions like this:

从"firebase-functions"导入功能;

我收到此错误:

!  TypeError: Cannot read property 'https' of undefined
    at Object.<anonymous> (C:\myProject\functions\index.js:28:55)

要修复它,我需要这样导入:

And to fix it, I need to import it like this:

import *作为"firebase-functions"中的函数;

虽然以下 import 对于 firebase-admin 正常工作:

While the following import works just fine for the firebase-admin:

从"firebase-admin"导入管理员;

问题

简而言之,问题是:

那是为什么:

import functions from 'firebase-functions';            // DOESN'T WORK
import * as functions from 'firebase-functions';       // WORKS
import admin from 'firebase-admin';                    // WORKS

推荐答案

从'firebase-functions'; 导入函数之所以不起作用的原因是'firebase-functions'没有默认的"功能"导出.

The reason why import functions from 'firebase-functions'; will not work is because 'firebase-functions' does not have a "functions" default export.

因此,此错误:

!  TypeError: Cannot read property 'https' of undefined
    at Object.<anonymous> (C:\myProject\functions\index.js:28:55)


解决方案:

第一种选择是导入整个模块的内容,并将 functions 添加到当前作用域中,该作用域包含模块 firebase-functions 的所有导出.


Solution:

The first option would be to import an entire module's contents and add functions into the current scope containing all the exports from the module firebase-functions.

import * as functions from 'firebase-functions'

第二种选择是从模块(在本例中为 https )导入单个导出,因为您正尝试读取'firebase的属性 https 功能的.

The second option would be to import a single export from a module, https in this case, since you are trying to read property https of 'firebase-functions'.

import { https } from 'firebase-functions'


可在此处找到更多信息

希望这可以澄清您的问题.

Hope this clarifies your question.

这篇关于如何使用ES6语法导入firebase-functions和firebase-admin以使用Babel进行Node 10的编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:57