【问题标题】:Firebase admin sdk setValue and setValueAsync are never completedFirebase admin sdk setValue 和 setValueAsync 从未完成
【发布时间】:2021-06-06 00:59:49
【问题描述】:

我正在尝试使用 firebase admin sdk 写入我的实时数据库。 我在用。但由于某种原因,请求没有执行。我正在使用斯卡拉。但我也觉得它与 java 相关,因为我刚刚将 java 代码转换为 scala。

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-admin</artifactId>
  <version>7.3.0</version>
</dependency>

我已正确设置 GOOGLE_APPLICATION_CREDENTIALS 并且其中的 auth 部分有效。我也将数据库的规则设置为,

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

代码:

 def updateFireBase(updated: WxResponseString, symbol: String): Future[Unit] ={
    println("start")

    try{
      val options = FirebaseOptions.builder.setCredentials(GoogleCredentials.getApplicationDefault)
        .setDatabaseUrl("https://xxxxxxxxxx.firebaseio.com/").build
      val app: FirebaseApp =FirebaseApp.initializeApp(options)
      println(app.getOptions.getDatabaseUrl)
      val ref = FirebaseDatabase.getInstance.getReference
      val symbolRef = ref.child("ticker/" + symbol.toLowerCase())
      println(symbolRef.toString)

      return Future{
        println("this is start")
        symbolRef.setValueAsync(updated.last).get() // gets stuck here forever
        println("this is end !!!!!!!!!!!!!")

      }
    } catch {
      case e:Exception => {
        logger.error("err", e)
      }
    }
    Future.successful()



  }

println("this is start") 被执行但println("this is end !!!!!!!!!!!!!") 没有被执行。我检查了 db url 是否正确,并通过调用 get 来确保线程被阻塞。 我经历了JavaEE and Firebase admin sdk - setValueAsync not pushing data to realtime firebase 和其他问题,但没有帮助。 我也用setValue 尝试了相同的代码,它的回调从未被调用。

【问题讨论】:

    标签: java firebase scala playframework firebase-admin


    【解决方案1】:

    你可以这样试试吗:

     def updateFireBase(updated: WxResponseString, symbol: String): Future[Unit] ={
        println("start")
    
        try{
          val options = FirebaseOptions.builder.setCredentials(GoogleCredentials.getApplicationDefault)
            .setDatabaseUrl("https://xxxxxxxxxx.firebaseio.com/").build
          val app: FirebaseApp =FirebaseApp.initializeApp(options)
          println(app.getOptions.getDatabaseUrl)
          val ref = FirebaseDatabase.getInstance.getReference
          val symbolRef = ref.child("ticker/" + symbol.toLowerCase())
          println(symbolRef.toString)
    
           ApiFuture<Void> future = symbolRef.setValueAsync(updated.last);
           future.get();
    
        } catch {
          case e:Exception => {
            logger.error("err", e)
          }
        }
        Future.successful()
      }
    

    【讨论】:

    • 我试过了,没用。这是FirebaseOptions.builder.setCredentials(GoogleCredentials.getApplicationDefault) 的问题,所以我将其更改为GoogleCredentials.fromStream(fileStream) 并且它有效。奇怪的是我在使用 getApplicationDefault 时没有收到任何错误
    • 嗯真的很奇怪。感谢分享解决方案:)
    猜你喜欢
    • 2023-03-20
    • 2018-03-31
    • 2020-02-26
    • 2018-10-22
    • 2016-10-13
    • 2018-03-20
    • 2019-05-15
    • 2018-03-11
    • 2017-11-22
    相关资源
    最近更新 更多