Vandana
Vandana

Reputation: 81

Consume Mono value and use it to call another Mono

My code is structured this way -

Mono<Address> m1 = method1() // this call returns address
Mono<Boolean> m2 = method2() // this call uses ReactiveMongoTemplate and updates document in Mongo

I am trying to achieve this :

when method1() returns me the address, I need to consume it and call method2() to update the address in a MongoDB document. No Exceptions thrown as well. But I don't see any logs inside method2()

Code :

Mono<Object> m1 = method1().map(address -> method2(address));

Although method2() is invoked, the document update in MongoDB is not happening.

Upvotes: 3

Views: 4781

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59221

Your code snippet is returning Mono<Mono<Boolean>>, so nothing is subscribing to the inner Mono.

You should probably use the Mono.flatMap operator like this:

Mono<Boolean> m1 = method1().flatMap(address -> method2(address));

This operator will flatten the chain of operations.

Upvotes: 5

Related Questions