繁体   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