簡體   English   中英

連接到 firebase/firestore 時出錯:無法到達 Cloud Firestore 后端

[英]Error when connecting to firebase/firestore: Could not reach Cloud Firestore backend

我正在 swift 開發一個應用程序並使用 Firestore 來存儲我的數據。 這是我第一次使用 Firebase。

昨天我將firebasefirestore連接到我的項目,一切正常。 今天突然報錯說無法連接到Firestore后端。

2020-04-03 13:37:25.851931+0200 Bouwresten[14277:2448929] 6.21.0 - [Firebase/Firestore][I-FST000001] 無法到達 Cloud Firestore 后端。 連接失敗 1 次。 最近的錯誤:網絡連接已更改這通常表示您的設備目前沒有健康的 Inte.net 連接。 客戶端將以離線模式運行,直到它能夠成功連接到后端。

現在我確定我的電腦連接良好,我正在使用 Xcode 的模擬器來測試應用程序。 有誰知道是什么問題?

這是我的 appdelegate.swift

//
//  AppDelegate.swift
//  Bouwresten
//
//  Created by Brecht Verhelst on 25/03/2020.
//  Copyright © 2020 Brecht Verhelst. All rights reserved.
//

import UIKit
import Firebase
import FirebaseFirestore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

這是我使用 firestore 獲取數據的 class,它叫做 dbhelper

//
//  DBHelper.swift
//  Bouwresten
//
//  Created by Brecht Verhelst on 02/04/2020.
//  Copyright © 2020 Brecht Verhelst. All rights reserved.
//

import Foundation
import Firebase
import FirebaseFirestore

class DBHelper
{
    var Users = [User]()
    var Products = [Product]()
    init() {
        getUsers()
    }

    func getUsers()  {
        Firestore.firestore().collection("Gebruikers").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    let user:User = User(Voornaam: document.get("Naam") as! String, Familienaam: document.get("Familienaam") as! String, Email: document.get("Email") as! String, Wachtwoord: document.get("Wachtwoord") as! String,
                                         Stad: document.get("Stad") as! String, Postcode: document.get("Postcode") as! String, Straat: document.get("Straat") as! String, Nummer: document.get("Nummer") as! String, Added_on: document.get("Added_on") as! String)
                    self.Users.append(user)
                    print(self.Users.count)
                }
            }
        }
    }

    func getProducts()  {
        Firestore.firestore().collection("Producten").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    let product:Product = Product(Titel: document.get("Titel") as! String, Beschrijving: document.get("Beschrijving") as! String, Prijs: document.get("Prijs") as! Double, Gereserveerd: document.get("Gereserveerd") as! Bool, Added_on: document.get("Added_on") as! String, Gebruiker_id: document.get("gebruiker_ID") as! String)
                    self.Products.append(product)
                }
            }
        }
        print(self.Products.count)
    }
}

希望有人能幫我解決這個問題。

你的代碼似乎沒問題。 一段時間以來,我對 XCode 模擬器有同樣的問題,但在真實設備上一切正常。 所以我認為這是一個可以忽略的 XCode 問題/錯誤。

這可能是最新版本的 Firebase (7.14.6) 中的一個錯誤,我嘗試切換到舊版本,它對我有用!

  1. 將 package.json 中的 Firebase 版本更改為:“firebase”:“7.14.6
  2. 然后添加base-64 package: https://www.npmjs.com/package/base-64
  3. 將此代碼段添加到您的 App.js:

 import {decode, encode} from'base-64'; if (.global.btoa) { global;btoa = encode. } if (.global;atob) { global.atob = decode; }

  1. 在項目的根文件夾中運行 npm install

  2. 再試一次,它應該工作!

我通過同步我的計算機和設備時間解決了這個錯誤。 他們離開了幾秒鍾(大約 15 秒鍾)。

為此,go 設置 -> 同步您的時鍾 -> 立即同步(Windows 10)。

就我而言,只有在調試器打開時才能訪問 firestore。 你可能想檢查一下。 在此處處理 React Native 項目。

您的代碼看起來沒有任何問題。

根據錯誤消息,您是否完全確定設備(如果您在模擬器上運行,則為 Mac/Macbook)具有良好的互聯網連接?

旁注:您只需為使用 Firebase 任何部分的所有文件import Firebase Firebase。

請更新您的規則

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read:  if request.auth != null;
      allow write: if false;
    }
  }
}

在此處查看詳細信息https://github.com/firebase/firebase-ios-sdk/issues/3763

我通過刪除我的 pod 文件並從我的應用程序中刪除我的 firebase 連接解決了這個問題。 重新安裝它,一切都很完美!

我剛剛遇到這個問題並通過...簡單地解決了它

allow write

在規則中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM