はじめに
次のように、location
フィールドに対してGeoPoint
のインスタンスを設定して、データを追加しようとしました。
val data = hashMapOf(
"createdAt" to FieldValue.serverTimestamp(),
"location" to GeoPoint(0.0, 0.0),
)
val collectionReference = firestore
.collection("records")
.document(recordId)
val reference = collectionReference
.add(data)
.await()
ところが、上記の処理を実行してみるとエラーが発生しました。
Found conflicting getters for name getDefaultInstanceForType on class com.google.type.LatLng
エラーの理由
これは、GeoPointはクラスであり、シリアライズとデシリアライズが不可能なためこのようなエラーが発生するということを表しています。
なので、次のようなルールを設定することはできません。
function isValidRecord(record) {
return record.size() == 2
&& "createdAt" in record && record.createdAt is timestamp
&& "location" in record && (record.location == null || record.location is latlng)
}
ところがどっこいの解決方法
ところが、いろいろ試していくとフィールド名にlocation
という名前を設定していたことが原因だということがわかりました。
なので、location
からposition
というフィールド名に変えてGeoPoint(0.0, 0.0)
を渡してみると、なんとエラーを発生させることなく成功しました。
val data = hashMapOf(
"createdAt" to FieldValue.serverTimestamp(),
"position" to GeoPoint(0.0, 0.0),
)