【问题标题】:How to create a Timestamp and Geopoint instance in Firebase Emulator from browser console如何从浏览器控制台在 Firebase 模拟器中创建时间戳和地理点实例
【发布时间】:2021-01-16 05:41:17
【问题描述】:

我创建了一个模拟数据并使用firestore.batch() 执行了批量更新。问题是位置保存为number,时间保存为string,而不是分别保存为 Geopoint 和 Timestamp 实例,导致我的 React 应用程序崩溃。

我可以看到只有firestore 可以通过window.firestore 访问,但不能通过Firebase 访问。由于 Firebase 未导出为 window.firebase,因此我无法创建 Geopoint 或 Timestamp 实例。

那么,如何通过浏览器控制台在 Firebase Emulator 中创建 Timestamp 和 Geopoint 实例?

这是我要添加到 firestore 的 doc 类型

const doc = {
  "company": "company-1",
  "location": [
    -72.3623, // number
    79.5748   // but, want to convert to Geopoint instance
  ],
  "time": "Fri Sep 10 1976 07:42:23 GMT+0530 (India Standard Time)",      // string
  "createdAt": "Mon Apr 28 2014 13:30:16 GMT+0530 (India Standard Time)", // want to convert to Timestamp
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    .Gf 更可靠的访问器路径:

    new firestore.app.firebase_.firestore.GeoPoint(lat, lng)

    【讨论】:

      【解决方案2】:

      可以通过window.firestore 访问firebase

      所以我想出了一种通过 Firestore 创建 Geopoint 和 Timestamp 实例的方法。

      您可以通过window.firestore.Gf.firebase_ 访问它,通过它您可以创建这两个实例。

      const raw = // pasting from clipboard
      const batch = firestore.batch()
      const firebase = firestore.Gf.firebase_
      
      const Timestamp = firebase.firestore.FieldValue().Timestamp
      const GeoPoint = firebase.firestore.FieldValue().GeoPoint
      
      raw.forEach(doc => {
        const docRef = firestore
          .collection('user')
          .doc('user-1')
          .collection('interviews')
          .doc()
        
        doc = {
          ...doc,
          time: Timestamp.fromDate(new Date(doc.time)),
          createdAt: Timestamp.fromDate(new Date(doc.createdAt)),
          location: new GeoPoint(doc.location[0], doc.location[1])
        }
        
        batch.set(docRef, doc)
      })
      
      batch.commit()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 2014-01-05
        • 2018-01-31
        • 2013-01-25
        相关资源
        最近更新 更多