【问题标题】:Anybody know how to make a search in a app that searches the database? [closed]有人知道如何在搜索数据库的应用程序中进行搜索吗? [关闭]
【发布时间】:2022-01-24 20:08:36
【问题描述】:

我正在快速开发一个应用程序,需要帮助。我试图在屏幕上显示一个搜索对象,当您键入字母时,它将使用 firebase 从实时数据库中获取结果。请帮忙。我不知道如何编码。请帮帮忙。

【问题讨论】:

    标签: swift xcode firebase firebase-realtime-database search


    【解决方案1】:

    您必须更具体地了解您正在尝试做什么。首先 - 您使用什么技术? JavaScript?什么框架?反应?角? Vuejs?香草 JavaScript?你正在做的是一个安静的应用程序还是服务器渲染的?

    根据您对上述问题的回答,代码可能会有很大差异。不过在这里我可以给你一个通用的解决方案:

    • 首先创建搜索框和一个带有发送请求的操作的按钮(主要是 HTML)
    • 该请求可以是基于特定搜索关键字在数据库中查找结果的 AJAX GET 请求。我推荐一个名为 axios 的库来帮助你。 (使用 JavaScript 发送和解析 Promise)
    • 处理该请求的响应,如果响应有效(状态为 200),则更新您的搜索结果。 (主要是 JavaScript)

    【讨论】:

      【解决方案2】:

      几周前我刚刚为我的应用程序做了这个,你很幸运!顺便说一句,我正在使用 swift 编程语言。

      因此,根据 firebase,您不应该使用 firebase 在数据库中搜索文档。 Firebase Documentation on this.。相反,您应该使用其他东西,例如“Algolia”、“Elastic”或“typeSense”。我选择使用 Algolia,我花了大约一天时间来实现我的应用程序。超级简单。

      但如果你真的想使用 firebase(不推荐),你可以使用这个:

          func loadPost(lastDoc: QueryDocumentSnapshot?, completion: @escaping (QueryDocumentSnapshot?) -> Void) {
          let db: Query
          // Check to see if previous documents have been loaded or not.
          if let lastDoc = lastDoc {
              db = Firestore.firestore().collection("collection name")
                  .order(by: "document name you are wanting to get. example: postTitle, username, date, etc")
                  // Amount of documents you want to retrieve from your colleciton
                  .limit(to: 10)
                  // Check certain conditions that you want to retrieve.
                  // In this example we are only retrieving the posts that have 100 reports or greater.
                  .whereField("reportCount", isGreaterThan: 100)
                  // Start after the previous loaded document so we don't waste data by reloading documents that have already been loaded.
                  .start(afterDocument: lastDoc)
              
              // If there are no previous loaded documents then you just start from the beginning of your collection colleciton.
          } else {
              db = Firestore.firestore().collection("posts")
                  .order(by: "document name", descending: true)
                  .limit(to: 10)
                  .whereField("document name", isGreaterThan: 100)
          }
          // finally get the documents you want from firebase
          db.getDocuments { query, error in
              
              // Check for errors
              if let query = query, error == nil {
                  
                  // Not really sure why you need a dispatchgroup, but its kind of like a completionHandler for a for loop.
                  let group = DispatchGroup()
                  for doc in query.documents {
                      group.enter()
                      // run the code you need to run:
                      
                      
                      // After the code is finished running you need to call group.leave()
                      group.leave()
                      }
                  // After the for loop is completely done call group.notify which is basically like a completionHandler
                  group.notify(queue: .main) {
                      // notify whatever needs to be notified.
                      
                      // finally insert the last document loaded so when we need to load data again it only loads documents that have not been loaded.
                      completion(query.documents.last)
                  }
              } else {
                  completion(nil)
              }
          }
      }
      

      【讨论】:

      • 非常感谢。我怎么把它放在搜索栏上?顺便说一句,这是我正在使用的数据库 (drive.google.com/file/d/1pSaHbBT4NpNUszpF7eQSO6b6A9TgKvL-/…) 我使用的是实时数据库而不是 firestore。谢谢
      • @user14963385 对不起,我没有看到您使用实时数据库,但我从未使用过实时数据库,所以我不知道。如果我是你,我会使用 Algolia。
      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 2019-10-03
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      相关资源
      最近更新 更多