7

Tôi đang cố nâng cấp từ phiên bản firebase trước lên phiên bản mới nhất trong số ionic project của mình. Tôi đã theo dõi hướng dẫn this để nâng cấp. Ở bước 4 từ trang này, tôi bị kẹt trong tuyên bố cuối cùng firebase.database().ref();.firebase.database không phải là chức năng

Thông báo lỗi

TypeError: firebase.database is not a function 

Dưới đây là mã của tôi. Giúp đỡ một cách tử tế.

... 

// Initialize Firebase 
this.config = { 
    apiKey: "some-api-key", 
    authDomain: "myapp.firebaseapp.com", 
    databaseURL: "https://myapp.firebaseio.com", 
    storageBucket: "project-somenumber.appspot.com", 
}; 

... 

this.authWithOAuthPopup = function(type) { 
    var deferred = $q.defer(); 
    console.log(service.config); // ---> Object {apiKey: "some-api-key", authDomain: "myapp.firebaseapp.com", databaseURL: "https://myapp.firebaseio.com", storageBucket: "project-somenumber.appspot.com"} 
    firebase.initializeApp(service.config); 
    console.log(firebase); // ---> Object {SDK_VERSION: "3.0.5", INTERNAL: Object} 
    service.rootRef = firebase.database().ref(); //new Firebase("https://rsb2.firebaseio.com"); ---> I am getting error on this line "TypeError: firebase.database is not a function" 
    service.rootRef.authWithOAuthPopup(type, function(error, authData) { 
     if (error) { 
      service.authError = error; 
      switch (error.code) { 
       case "INVALID_EMAIL": 
        console.log("The specified user account email is invalid."); 
        break; 
       case "INVALID_PASSWORD": 
        console.log("The specified user account password is incorrect."); 
        break; 
       case "INVALID_USER": 
        console.log("The specified user account does not exist."); 
        break; 
       default: 
        console.log("Error logging user in:", error); 
      } 
      deferred.resolve(service.authError); 
     } else { 
      service.authData = authData; 
      console.log("Authenticated successfully with payload:", authData); 
      deferred.resolve(service.authData); 
     } 
     return deferred.promise; 
    }); 
    return deferred.promise; 
} 

var service = this; 

Cập nhật

Sau khi thêm thư viện cơ sở dữ liệu mới nhất vấn đề thắc mắc này được giải quyết.

Cập nhật mã của tôi ở đây

this.authWithOAuthPopup = function(type) { 
    var deferred = $q.defer(); 
    console.log(service.config); 
    firebase.initializeApp(service.config); 
    console.log(firebase); 
    service.rootRef = firebase.database(); //.ref(); //new Firebase("https://rsb2.firebaseio.com"); 

    var provider = new firebase.auth.FacebookAuthProvider(); 
    firebase.auth().signInWithRedirect(provider); 
    firebase.auth().getRedirectResult().then(function(result) { 
     if (result.credential) { 
      // This gives you a Facebook Access Token. You can use it to access the Facebook API. 
      var token = result.credential.accessToken; 
      console.log(result); 
      // ... 
     } 
     // The signed-in user info. 
     var user = result.user; 
    }).catch(function(error) { 
     // Handle Errors here. 
     var errorCode = error.code; 
     var errorMessage = error.message; 
     // The email of the user's account used. 
     var email = error.email; 
     // The firebase.auth.AuthCredential type that was used. 
     var credential = error.credential; 
     // ... 
    }); 
    return deferred.promise; 
} 
+0

cho tôi biết nếu bạn thực hiện bất kỳ sự tiến bộ – adolfosrs

Trả lời

7

Tôi gặp vấn đề này với Ionic và hóa ra tôi chưa bao gồm mọi thứ khi sử dụng Ứng dụng khách Firebase mới nhất. Nếu bạn đã bao gồm Firebase là firebase-app thì các phần Cơ sở dữ liệu và Xác thực cần được yêu cầu riêng biệt vì chúng không được nhóm khi bao gồm Firebase theo cách này.

Thêm dòng sau vào index.html của bạn sau khi bạn bao gồm firebase-app.js

<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-auth.js"></script> 
<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-database.js"></script> 

Rõ ràng bạn không cần phải sử dụng CDN, bạn có thể sử dụng bower (có lẽ là cách ưa thích với Ionic) hoặc NPM với Browserify.

// Browserify Setup 
var firebase = require('firebase/app'); 
require('firebase/auth'); 
require('firebase/database'); 

Snippet dưới đây lấy từ Firebase Web Setup Docs

Bạn có thể giảm số lượng mã ứng dụng của bạn sử dụng bằng cách chỉ bao gồm các tính năng bạn cần. Các thành phần có thể cài đặt riêng lẻ là:

firebase-app - Ứng dụng khách Firebase lõi (bắt buộc).
firebase-auth - Xác thực Firebase (tùy chọn).
cơ sở dữ liệu Firebase - Cơ sở dữ liệu thời gian thực Firebase (tùy chọn).
lưu trữ Firebase - Bộ nhớ Firebase (tùy chọn).

Từ CDN, bao gồm các thành phần riêng lẻ bạn cần (bao gồm căn cứ hỏa lực ứng dụng đầu tiên)

+0

Cảm ơn tôi đã bao gồm thư viện cơ sở dữ liệu. Nó hoạt dộng bây giờ. – Ricky

1

Trước tiên, hãy chắc chắn rằng bạn đang sử dụng

<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script> 

căn cứ hỏa lực authWithOAuthPopup đã thay đổi một chút trong phiên bản mới.

Bây giờ bạn không sử dụng phương thức xác thực để gọi. Bạn nên sử dụng firebase.auth() được insted.

var auth = firebase.auth(); 
var provider = new firebase.auth.TwitterAuthProvider(); 
auth.signInWithPopup(provider).then(function(result) { 
    // User signed in! 
    var uid = result.user.uid; 
}).catch(function(error) { 
    // An error occurred 
}); 
+0

Các [doc] (https://firebase.google.com/ docs/auth/web/facebook-login) đề xuất sử dụng phương pháp chuyển hướng cho thiết bị di động.Vì vậy, tôi có thể xác thực bằng cách sử dụng facebook bằng cách sử dụng phương pháp chuyển hướng, nhưng điều này tải lại trang web của tôi (hiện đang thử nghiệm ứng dụng ion trên chrome) và tôi không thể có được các chi tiết tải trọng. Tôi đã cập nhật mã của mình ở trên – Ricky

Các vấn đề liên quan