【问题标题】:Use Value of Result Mono from DB Request in Another Request来自 DB 请求的结果 Mono 在另一个请求中的使用价值
【发布时间】:2019-09-19 05:05:05
【问题描述】:

我想通过用户名获取用户,将该用户指定为笔记的作者,然后将该笔记保存到数据库中。问题是我得到了一个包含用户的 Mono,我无法将其分配给用户类型的作者字段。

我正在尝试做的事情:

noteRepository
    .save(noteMapper
        .fromDTO(noteDTO)
        .apply { owner = userReository
            .findByUsername(userPrincipalService.getCurrentUserPrincipal()
                .map { it.username }) 
        })

【问题讨论】:

    标签: spring spring-data spring-webflux project-reactor spring-mongodb


    【解决方案1】:
    userRepository
            // Find the User
            .findByUsername(userPrincipalService.getCurrentUserPrincipal()
                    .map { it.username })
            // Map the User to a Note, while setting the note's owner 
            .map { user ->
                noteMapper.fromDTO(noteDTO).apply {
                    owner = user
                }
            }
            // Save the Note 
            .flatMap { note -> noteRepository.save(note) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      相关资源
      最近更新 更多