Home > Enterprise >  ReactiveMongoRepository can't save to database
ReactiveMongoRepository can't save to database

Time:12-22

I'm newbie with Java. I'm create an API with Webflux and ReactiveMongoRepository. I try to save my data from another API.

My repository:

@Repository
public interface AccountApiTrackingRepo extends ReactiveMongoRepository<AccountApiTracking, String> {
}

My service implements:

    @Override
    public void findByIdAccountsApiTrack(User user, String accountId){
        AccountApiTracking accountApiTracking = new AccountApiTracking();

        accountApiTracking.setUser(user);
        accountApiTracking.setAccountId(accountId);
        accountApiTracking.setAction("Find account by Id");
        System.out.println(accountApiTracking);
        accountApiTrackingRepo.save(accountApiTracking);
    }

My services:

@Service
public interface AccountApiTrackingService {

    public void createAccountsApiTrack(User user, AccountDto accountDto);
    public void findByIdAccountsApiTrack(User user, String accountId);

    Flux<AccountApiTrackingDto> findAll();
}

My model:

@Document
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AccountApiTracking implements AutoMapping<AccountApiTrackingDto> {
    @Id
    private String id;
    private User user;
    private String action;
    private AccountDto payload;
    private String accountId;

    @Override
    public String toString() {

        return String.valueOf(action);
    }
}

After function findByIdAccountsApiTrack() run I can't find any document created in my database. I can see my variable accountApiTracking have data. But accountApiTrackingRepo.save doesn't work. What I'm missing?

CodePudding user response:

In Reactive programming should subscribe to any reactive instruction. you should return accountApiTrackingRepo.save(accountApiTracking); to till your usage for example in the controller. or for your test case, you can use ".subscribe()" after your method call.

  • Related