简体   繁体   English

firebase.database()不是本地节点上的函数

[英]firebase.database() is not a function on local node

I'm running a simple node app in my local machine and I need to connect to firebase realtime database. 我在本地计算机上运行一个简单的节点应用程序,我需要连接到Firebase实时数据库。

I installed firebase via npm: 我通过npm安装了firebase:

npm install firebase --save

Then I initialize the app: 然后,我初始化应用程序:

var firebase = require("firebase");

var config = {
    apiKey: "api-key",
    authDomain: "my-app-database.firebaseapp.com",
    databaseURL: "https://my-url.firebaseio.com",
    storageBucket: "my-app-database.appspot.com",
};

firebase.initializeApp(config);

var myRef = firebase.database().ref("collection").on("value", (snap) => {
  // do something with the data
});

Then I get the error that database is not a function. 然后我得到一个错误,即数据库不是一个函数。 I check firebase.database and is undefined, also so are firebase.auth and firebase.storage . 我检查firebase.database并且它是未定义的, firebase.authfirebase.storage也是未定义的。

I followed all the steps in the docs, but I don't see anything that could be causing this. 我按照文档中的所有步骤进行操作,但没有发现任何可能导致此问题的原因。

Goodness gracious... It was as simple as requiring the other packages in the file, like this: 天哪,这就像在文件中需要其他软件包一样简单:

// firebase
const firebase = require("firebase");
// get database, auth and storage
require("firebase/auth");
require("firebase/storage");
require("firebase/database");

Nowhere in the docs or reference says that. 在文档或参考中没有任何地方可以这么说。 I thought about going back a version, perhaps 4.12.x, so I went to the npm page to see the previous versions and install one of those and try, when I found this: 我考虑过要返回一个版本,也许是4.12.x,所以我在npm页面上看到了以前的版本并安装了其中一个版本,然后在发现以下内容时尝试:

https://www.npmjs.com/package/firebase#include-only-the-features-you-need https://www.npmjs.com/package/firebase#include-only-the-features-you-need

Just scroll down where they mention using npm packages or Typescript and you'll find the answer. 只需使用npm软件包或Typescript向下滚动他们提到的地方,即可找到答案。

Shout out to the Firebase team, this information can't be just in the npm page and not in the docs, getting started guides or the github repo. 向Firebase团队大喊大叫,此信息不能仅出现在npm页面上,而不能在文档,入门指南或github存储库中。 Not a lot of people goes to the npm page of a package for information and I went there to check previous versions, so I kind of stumbled upon it. 没有很多人去查看软件包的npm页面,所以我去那里检查了以前的版本,因此我偶然发现了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM