简体   繁体   English

Spring WebFlux:Spring Data Reactive MongoDB中的block()方法返回null

[英]Spring WebFlux: block() method return null in Spring Data Reactive MongoDB

i'm trying to learn project Reactor and have the problem. 我正在尝试学习Reactor项目并遇到问题。

@Test
@DisplayName("check that standaloneUser fields are correct")
void validateUserFields() {
    userService.save(standaloneUser).subscribe();
    assertEquals(userService.count().block(), Long.valueOf(1));
    User user = userService.findByEmail("test@gmail.com").block();
    assertNotNull(user);
    assertNotNull(user.getId());
    assertEquals(user.getFirstName(), "test");
    assertEquals(user.getLastName(), "test");
    assertNotEquals(user.getPassword(), "test");
    assertEquals(user.getRole(), Role.CANDIDATE);
    assertNotNull(user.getCreatedDate());
    assertNull(user.getStoppedDate());
    assertEquals(user.getEmail(), "test@gmail.com");
}

Sometimes block() method returns null. 有时,block()方法返回null。 Who can explain me this? 谁能解释这个? Thanks 谢谢

block() can return null, it means the Mono completed empty, which in this case means the user wasn't found. block() 可以返回null,这意味着Mono已完成为空,在这种情况下,这意味着找不到用户。

Could it be that it wasn't properly saved? 可能是由于未正确保存? (although you assert the user count) (尽管您断言用户数)

Note that you do userService.save(standaloneUser).subscribe() . 请注意,您需要执行userService.save(standaloneUser).subscribe() This form is often not ideal, as it is "async fire-and-forget": 这种形式通常不是理想的,因为它是“异步即发即弃”的:

  • async -> it might complete after the subsequent asserts 异步->它可能后续断言之后完成
  • fire and forget -> no error handler means that it could terminate with an error and hide it from you . 解雇->没有错误处理器意味着它可能会因错误而终止并向您隐藏

Make an habit of at least setting onNext and onError handler lambdas when calling subscribe . 养成至少在调用subscribe时设置onNext和onError处理程序lambdas的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring WebFlux:在Spring Data MongoDB反应库中发出null值的异常? - Spring WebFlux: Emit exception upon null value in Spring Data MongoDB reactive repositories? WebFlux Spring Boot @Transactional 与反应式 MongoDB - WebFlux Spring Boot @Transactional with reactive MongoDB 更新 1 个或多个特定字段 MongoDB 使用 Spring 启动 WebFlux,Spring 数据 Z206E3718AF0921CC1D12F80ZReposit7 - Update 1 or multiple specific field MongoDB using Spring boot WebFlux,Spring Data MongoDB Reactive and ReactiveMongoRepository 在Maven中看不到spring-boot-starter-data-mongodb-active和spring-boot-starter-webflux依赖性 - Dont see spring-boot-starter-data-mongodb-reactive and spring-boot-starter-webflux dependency in maven Webflux数据检查与Mongo反应式弹簧 - Webflux data check with mongo reactive spring object 的反应流(Spring Webflux)中的 .block() 包含 state - .block() within reactive flow (Spring Webflux) for object that holds state Spring WebFlux块Http.CONNECT方法 - Spring WebFlux block Http.CONNECT method 审核(@CreatedDate)不适用于带有反应式 MongoDB 的 WebFlux Spring Boot - Auditing (@CreatedDate) does not work for WebFlux Spring Boot with reactive MongoDB 使用 Spring WebFlux & Reactive MongoDB 无法运行测试 - Tests do not run using Spring WebFlux & Reactive MongoDB Spring Boot Webflux 反应式 API - Spring Boot Webflux reactive api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM