mongodb - 出现错误:(55, 41) Kotlin:类型推断失败
问题描述
我明白了Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is (Mutable)List<Optional<Address!>!>! but List<Address>? was expected
我将数据保存到我的 build.gradle 具有的 mongoDB 文档中:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
我的文档对象
@Document(collection = "address")
class Address {
@Id
var id: String? = null
var number: String? = null
var street: String? = null
var neighborhood: String? = null
var locality: String? = null
var region: String? = null
var country: String? = null
var code: String? = null
var google_place_id: String? = null
constructor()
}
和
@Document(collection = "person")
class Person {
@Id
var id: String? = null
var name: String? = null
var lastName: String? = null
var phone: String? = null
var email: String? = null
var password: String? = null
@DBRef(db = "mlm")
var address: List<Address>? = null
constructor()
}
我有接口
@Repository
interface PersonRepository : MongoRepository<Person, String>
和
@Repository
interface AddressRepository : MongoRepository<Address, String>
我的控制器工作
我的播种机/井测试真的看看如何做到这一点看起来像这样,这就是我遇到问题的地方
class DbSeeder {
@Autowired
private val personRepository: PersonRepository? = null
@Autowired
private val addressRepository: AddressRepository? = null
fun addressLoading() {
val address1 = Address()
address1.id = "5cb2e9424274072ec4bb4199"
address1.number = "1"
address1.street = "Microsoft Way"
address1.neighborhood = "Redmond"
address1.locality = "King County"
address1.region = "Washington"
address1.code = "425"
address1.country = "United States"
address1.google_place_id = "5644+83 Centurion"
val address2 = Address()
address2.id = "5cb2e9424274072ec4bb4198"
address2.number = "1600"
address2.street = "Amphitheatre Parkway"
address2.neighborhood = ""
address2.locality = "Mountain View"
address2.region = "California"
address2.country = "United States"
address2.code = "94043"
address2.google_place_id = "CWC8+Q9 Mountain View, California, USA"
val address = Arrays.asList(address1, address2)
this.addressRepository!!.insert(address)
}
fun personLoading() {
val personDocument = Person()
personDocument.id = "5cb2e9424274072ec4bb4197"
personDocument.name = "William"
personDocument.lastName = "Gates"
personDocument.phone = "1081010810"
personDocument.email = "bill.gates@gmail.com"
personDocument.password = "bill-secret"
val personAddressDBRef = addressRepository!!.findById("5cb2e9424274072ec4bb4199")
personDocument.address = Arrays.asList(personAddressDBRef)
// val personDBRef = personRepository!!.save(personDocument) // If the ObjectID is requires else ware
personRepository!!.save(personDocument)
}
}
要保存我的地址,我需要找到与 ObjectID 相关的地址,然后将 ObjectID 保存为个人文档中的 DBRef
val personAddressDBRef = addressRepository!!.findById("5cb2e9424274072ec4bb4199")
personDocument.address = Arrays.asList(personAddressDBRef) // << Error here
在最后一行我收到错误Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is (Mutable)List<Optional<Address!>!>! but List<Address>? was expected
我似乎不知道如何继续
我在 GitHub 上提供了它https://github.com/Morons/gofetchbyidandinsertdbref.git
任何帮助将不胜感激
解决方案
您需要阅读https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types以了解推断类型的编写方式。但问题很简单:
personAddressDBRef
有类型Optional<Address>
,因为那是findById
返回的。Arrays.asList(personAddressDBRef)
也是如此List<Optional<Address>>
(再次,请参阅上面的链接了解为什么您会看到更复杂的类型)。您不能设置
personDocument.address
为 aList<Optional<Address>>
,而是需要 aList<Address>
。
因此,您需要决定如何将 a 转换Optional<Address>
为List<Address>
. 一种方法是
personDocument.address = personAddressDBRef.map { listOf(it) }.orElseGet { listOf() }
推荐阅读
- c# - 当多个用户尝试使用 asp.net mvc 5 更新最新详细信息时,如何以相同的形式保存多个数据
- python - 如何以 timedelta 格式绘制带有天数和时间的时间分布直方图 - python?
- python - 在 python 程序中同时使用 csv 和文本文件是个好主意吗?
- python - 访问列表字典的更快方法?
- python-3.x - Django 聚合 ForeignKey 层次结构
- go - 如何复制openssl命令解密cms文件
- sql - SQL Server:比较两个表中的时间并在行匹配后从匹配过程中删除
- firebase - 颤振 - Cloud Firestore 0.14 中 Future.builder 的问题
- amazon-web-services - 如何在 aws cloudfront 中更新/替换 robots.txt 文件
- python - 如何在函数外部对变量进行函数处理 - 基于它的名称作为字符串