为什么会出现错误“ firebase.database不是函数”?

时间:2019-11-05 16:02:50

标签: javascript angularjs firebase firebase-realtime-database

我试图学习如何与angularjs聊天,为此,我发现此网站https://medium.com/@maheshkariya/create-a-simple-chat-system-using-angularjs-and-firebase-360cfcf2b34尝试了代码,但是它已经很旧了。我尝试更新它,但是卡住了。
html文件中的头文件有:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js">
</script>
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-firestore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angularFire/2.3.0/angularfire.min.js"></script>
<script src="js/script.js"></script>

在script.js中:

(function() {
    "use strict";
    angular.module('chat-app', ['firebase']);
    angular.module('chat-app')
        .factory('Messages', Messages);
    Messages.$inject = ['$firebaseArray'];
    function Messages($firebaseArray) {
        return function() {
            var config = {
                apiKey: "******************",
                authDomain: "*************",
                databaseURL: "****************"
            }; 
            firebase.initializeApp(config);
            var rootRef = firebase.database().ref();
            return $firebaseArray(rootRef);                
        }
    }
    angular.module('chat-app')
        .controller('ChatCtrl', ChatCtrl);
    ChatCtrl.$inject = ['Messages'];
    function ChatCtrl(Messages) {
        var self = this;
        self.messages = Messages();
       self.saveMessage = function() {
           if (self.name && self.message) {
               self.messages.$add({ name: self.name, text: self.message });
               self.message = null;
            } else { }
        };

    }

})();

在控制台中出现错误:

TypeError: firebase.database is not a function
at pen.js:28
at ChatCtrl (pen.js:43)
at Object.invoke (angular.js:5141)
at S.instance (angular.js:11704)
at p (angular.js:10517)
at g (angular.js:9832)
at g (angular.js:9835)
at angular.js:9697
at angular.js:1965
at m.$eval (angular.js:19393)

如何解决?

我尝试在html文件中添加https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js脚本,但也收到错误消息:

Error: "permission_denied at /: Client doesn't have permission to access the desired data."
e https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onComplete https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
startListening https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
sendListen_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onDataMessage_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onDataMessage_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onPrimaryMessageReceived_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
connReceiver_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
t https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
wt https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
t https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
n https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
handleResponse https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
scriptTagHolder https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
pRTLPCB https://s-usc1c-nss-209.firebaseio.com/.lp?start=t&ser=25217500&cb=1&v=5&ns=chatbot-ad582:6
<anonymous> https://s-usc1c-nss-209.firebaseio.com/.lp?id=11085377&pw=DjtoYaTDIL&ser=5478690&ns=chatbot-ad582&seg0=1&ts0=1&d0=eyJ0IjoiZCIsImQiOnsiciI6MiwiYSI6InEiLCJiIjp7InAiOiIvIiwiaCI6IiJ9fX0.:1
angular.min.js:127:303
Error: "permission_denied at /: Client doesn't have permission to access the desired data."
e https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onComplete https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
startListening https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
sendListen_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onDataMessage_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onDataMessage_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
onPrimaryMessageReceived_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
connReceiver_ https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
t https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
wt https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
t https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
n https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
handleResponse https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
scriptTagHolder https://www.gstatic.com/firebasejs/7.2.3/firebase-database.js:1
pRTLPCB https://s-usc1c-nss-209.firebaseio.com/.lp?start=t&ser=25217500&cb=1&v=5&ns=chatbot-ad582:6
<anonymous> https://s-usc1c-nss-209.firebaseio.com/.lp?id=11085377&pw=DjtoYaTDIL&ser=5478690&ns=chatbot-ad582&seg0=1&ts0=1&d0=eyJ0IjoiZCIsImQiOnsiciI6MiwiYSI6InEiLCJiIjp7InAiOiIvIiwiaCI6IiJ9fX0.:1
 Possibly unhandled rejection: {"code":"PERMISSION_DENIED"}

1 个答案:

答案 0 :(得分:0)

在firebase上,转到应用程序控制台,然后在数据库上创建实时数据库(以前是自动创建的,现在必须手动创建)。