简体   繁体   English

Firebase.database()不是函数-Sails Service

[英]Firebase.database() is not a function - Sails Service

I've made a service called Firebase.js, and I am then trying to call this from my Controllers using the likes of Firebase.database , however I get the error Firebase.database() is not a function 我已经提供了一个名为Firebase.js的服务,然后尝试使用Firebase.database从我的控制器中调用它,但是我收到错误Firebase.database()不是一个函数

services/Firebase.js 服务/Firebase.js

var admin = require("firebase-admin");

var instance = admin.initializeApp({
        credential: admin.credential.cert({
          [[cert info]]
        }),
        databaseURL: "foo.com"
    })

module.exports.default = instance;

Controller 控制者

    var db = Firebase.database();
    var ref = db.ref("my-db");
    var brandsRef = ref.child("brands");

    brandsRef.once("value", function(data){
        console.log(data);
    })

I'm not too sure where I am going wrong. 我不太确定我要去哪里哪里。 Any suggestions? 有什么建议么?

services/Firebase.js 服务/Firebase.js

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

var instance = admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://YOUR_DATABASE_NAME.firebaseio.com"
});

module.exports.firebaseDB = instance.database();// Export the database object

Controller 控制者

//Don't need initialize the Firebase again, only call function from service
var ref = Firebase.firebaseDB.ref("/your_resource");
ref.once("value", function(snapshot) {
  return res.json({MY_RESOURCE_DATA: snapshot.val()});
});

Where Firebase is the service file name and firebaseDB is the database object that export for use in the service. 其中, Firebase是服务文件名, firebaseDB是导出以在服务中使用的数据库对象。

In your controller you should be initializing the database for usage. 在您的控制器中,您应该初始化数据库以供使用。 The code looks similar to this: 该代码类似于以下内容:

var config = {
    apiKey: "Sample api key here",
    authDomain: "yourapplication.firebaseapp.com",
    databaseURL: "yourapplication.firebaseio.com",
    storageBucket: "yourapplication.appspot.com",
    messagingSenderId: "Id number here"
  };
firebaseObject.initializeApp(config);

firebaseObject would then be the object that you use for the database reference, like so: 然后,firebaseObject将成为您用于数据库引用的对象,如下所示:

var db = firebaseObject.database();
var ref = db.ref("my-db");

也许问题是您使用的是大写字母,请使用firebase.database()

in the controller you have to do 在控制器中,您必须做

var db = Firebase.database();
var ref = db.ref("my-db");
var brandsRef = ref.child("brands");

brandsRef.once('value').then{function(snapshot){
    console.log(snapshot.val()._name data from firebase__);
});

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

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